From: Michael M Slusarz Date: Thu, 9 Sep 2010 23:04:14 +0000 (-0600) Subject: Implement ArrayAccess for Horde_Mime_Part X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=9922f1a17c400ca28f3c6eb45613e12cf0f14586;p=horde.git Implement ArrayAccess for Horde_Mime_Part --- diff --git a/framework/Mime/lib/Horde/Mime/Part.php b/framework/Mime/lib/Horde/Mime/Part.php index cf39b5484..c8ca1d380 100644 --- a/framework/Mime/lib/Horde/Mime/Part.php +++ b/framework/Mime/lib/Horde/Mime/Part.php @@ -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. */ /** diff --git a/framework/Mime/test/Horde/Mime/PartTest.php b/framework/Mime/test/Horde/Mime/PartTest.php index 3a1603207..2855a1d33 100644 --- a/framework/Mime/test/Horde/Mime/PartTest.php +++ b/framework/Mime/test/Horde/Mime/PartTest.php @@ -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();