Fix alterPart() and removePart().
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 18 Nov 2008 20:58:20 +0000 (13:58 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 18 Nov 2008 20:58:20 +0000 (13:58 -0700)
framework/Mime/lib/Horde/Mime/Part.php

index 3a1b2a6..022caca 100644 (file)
@@ -566,73 +566,85 @@ class Horde_Mime_Part
     /**
      * Retrieve a specific MIME part.
      *
-     * @param string $id  The MIME ID.
+     * @param string $id  The MIME ID to get.
      *
-     * @return Horde_Mime_Part  A pointer to the part requested, or null if
-     *                          the part doesn't exist.
+     * @return Horde_Mime_Part  The part requested or null if the part doesn't
+     *                          exist.
      */
-    public function &getPart($id)
+    public function getPart($id)
     {
-        $this_id = $this->getMimeId();
-        /* Need strcmp() because, e.g., '2.0' == '2'. */
-        if (strcmp($id, $this_id) === 0) {
-            return $this;
-        }
-
-        if ($this->_reindex) {
-            $this->buildMimeIds(is_null($this_id) ? '1' : $this_id);
-        }
-
-        return $this->_partFind($id, $this->_parts);
+        return $this->_partAction($id, 'get');
     }
 
     /**
      * Remove a subpart.
      *
-     * @param string $id  The MIME part to delete.
+     * @param string $id  The MIME ID to delete.
      */
     public function removePart($id)
     {
-        $ob = &$this->getPart($id);
-        if ($ob !== null) {
-            unset($ob);
-        }
+        $this->_partAction($id, 'remove');
     }
 
     /**
      * Alter a current MIME subpart.
      *
-     * @param string $id                  The MIME part ID to alter.
+     * @param string $id                  The MIME ID to alter.
      * @param Horde_Mime_Part $mime_part  The MIME part to store.
      */
     public function alterPart($id, $mime_part)
     {
-        $ob = &$this->getPart($id);
-        if ($ob !== null) {
-            $ob = $mime_part;
-        }
+        $this->_partAction($id, 'alter', $mime_part);
     }
 
     /**
-     * Function used to find a specific MIME part by ID.
+     * Function used to find a specific MIME part by ID and perform an action
+     * on it.
      *
-     * @param string $id     The MIME ID.
-     * @param array &$parts  A list of Horde_Mime_Part objects.
+     * @param string $id                  The MIME ID.
+     * @param string $action              The action to perform ('get',
+     *                                    'remove', or 'alter').
+     * @param Horde_Mime_Part $mime_part  The object to use for 'alter'.
      *
-     * @return mixed  A pointer to the Horde_Mime_Part object, or null if the
-     *                object is not found.
+     * @return mixed  For 'get', a pointer to the Horde_Mime_Part object, or
+     *                null if the object is not found.
      */
-    protected function &_partFind($id, &$parts)
+    protected function _partAction($id, $action, $mime_part = null)
     {
-        foreach (array_keys($parts) as $val) {
-            $ret = $parts[$val]->getPart($id);
-            if (!is_null($ret)) {
-                return $ret;
+        $this_id = $this->getMimeId();
+
+        /* Need strcmp() because, e.g., '2.0' == '2'. */
+        if (($action == 'get') && (strcmp($id, $this_id) === 0)) {
+            return $this;
+        }
+
+        if ($this->_reindex) {
+            $this->buildMimeIds(is_null($this_id) ? '1' : $this_id);
+        }
+
+        foreach (array_keys($this->_parts) as $val) {
+            $partid = $this->_parts[$val]->getMimeId();
+            if (strcmp($id, $partid) === 0) {
+                switch ($action) {
+                case 'alter':
+                    $mime_part->setMimeId($this->_parts[$val]->getMimeId());
+                    $this->_parts[$val] = $mime_part;
+                    return;
+
+                case 'get':
+                    return $this->_parts[$val];
+
+                case 'remove':
+                    unset($this->_parts[$val]);
+                    $this->_reindex = true;
+                    return;
+                }
+            } elseif (strpos($id, $partid) === 0) {
+                return $this->_parts[$val]->_partAction($id, $action, $mime_part);
             }
         }
 
-        $ret = null;
-        return $ret;
+        return null;
     }
 
     /**