Implement ArrayAccess for Horde_Mime_Part
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 9 Sep 2010 23:04:14 +0000 (17:04 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 9 Sep 2010 23:10:10 +0000 (17:10 -0600)
framework/Mime/lib/Horde/Mime/Part.php
framework/Mime/test/Horde/Mime/PartTest.php

index cf39b54..c8ca1d3 100644 (file)
@@ -14,7 +14,7 @@
  * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
  * @package  Mime
  */
-class Horde_Mime_Part implements Countable
+class Horde_Mime_Part implements ArrayAccess, Countable
 {
     /* The character(s) used internally for EOLs. */
     const EOL = "\n";
@@ -2101,6 +2101,28 @@ class Horde_Mime_Part implements Countable
         return $out;
     }
 
+    /* ArrayAccess methods. */
+
+    public function offsetExists($offset)
+    {
+        return ($this->getPart($offset) !== null);
+    }
+
+    public function offsetGet($offset)
+    {
+        return $this->getPart($offset);
+    }
+
+    public function offsetSet($offset, $value)
+    {
+        $this->alterPart($offset, $value);
+    }
+
+    public function offsetUnset($offset)
+    {
+        $this->removePart($offset);
+    }
+
     /* Countable methods. */
 
     /**
index 3a16032..2855a1d 100644 (file)
@@ -82,6 +82,44 @@ class Horde_Mime_PartTest extends PHPUnit_Framework_TestCase
         );
     }
 
+    public function testArrayAccessImplementation()
+    {
+        $part = $this->_getTestPart();
+
+        $this->assertEquals(
+            true,
+            isset($part['1'])
+        );
+        $this->assertEquals(
+            false,
+            isset($part['4'])
+        );
+
+        $this->assertSame(
+            $part->getPart('1'),
+            $part['1']
+        );
+        $this->assertSame(
+            $part->getPart('3.1'),
+            $part['3.1']
+        );
+
+        $part2 = new Horde_Mime_Part();
+        $part2->setType('text/plain');
+
+        $part['2'] = $part2;
+        $this->assertSame(
+            $part2,
+            $part->getPart('2')
+        );
+
+        unset($part['3']);
+        $this->assertEquals(
+            null,
+            $part->getPart('3')
+        );
+    }
+
     public function testCountableImplementation()
     {
         $part = $this->_getTestPart();