From da6794d05578e2ff74ee361996eeae1febfd928e Mon Sep 17 00:00:00 2001 From: Ben Scholzen Date: Sat, 14 May 2011 12:13:17 +0200 Subject: [PATCH] Added ArrayAccess to Zend\Config\Config --- src/Config.php | 51 ++++++++++++++++++++++++++++++++++++++++++++- test/ConfigTest.php | 19 +++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/Config.php b/src/Config.php index 78593ff..280e4b2 100644 --- a/src/Config.php +++ b/src/Config.php @@ -30,7 +30,7 @@ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class Config implements \Countable, \Iterator +class Config implements \Countable, \Iterator, \ArrayAccess { /** * Whether in-memory modifications to configuration data are allowed @@ -306,6 +306,55 @@ public function valid() return $this->_index < $this->_count; } + /** + * offsetExists(): defined by ArrayAccess interface. + * + * @see ArrayAccess::offsetExists() + * @param mixed $offset + * @return boolean + */ + public function offsetExists($offset) + { + return $this->__isset($offset); + } + + /** + * offsetGet(): defined by ArrayAccess interface. + * + * @see ArrayAccess::offsetGet() + * @param mixed $offset + * @return mixed + */ + public function offsetGet($offset) + { + return $this->__get($offset); + } + + /** + * offsetSet(): defined by ArrayAccess interface. + * + * @see ArrayAccess::offsetSet() + * @param mixed $offset + * @param mixed $value + * @return void + */ + public function offsetSet($offset, $value) + { + $this->__set($offset, $value); + } + + /** + * offsetUnset(): defined by ArrayAccess interface. + * + * @see ArrayAccess::offsetUnset() + * @param mixed $offset + * @return void + */ + public function offsetUnset($offset) + { + $this->__unset($offset); + } + /** * Returns the section name(s) loaded. * diff --git a/test/ConfigTest.php b/test/ConfigTest.php index 53d121c..3e7cf2e 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -316,6 +316,25 @@ public function testMerge() $this->assertEquals(456, $stdConfig->{2}); } + + public function testArrayAccess() + { + $config = new Config($this->_all, true); + + $this->assertEquals('thisname', $config['name']); + $config['name'] = 'anothername'; + $this->assertEquals('anothername', $config['name']); + $this->assertEquals('multi', $config['one']['two']['three']); + + $this->assertTrue(isset($config['hostname'])); + $this->assertTrue(isset($config['db']['name'])); + + unset($config['hostname']); + unset($config['db']['name']); + + $this->assertFalse(isset($config['hostname'])); + $this->assertFalse(isset($config['db']['name'])); + } /** * Ensures that toArray() supports objects of types other than Zend_Config