diff --git a/src/Config.php b/src/Config.php index f854d9a..d1a6c8b 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 @@ -304,6 +304,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. * @@ -442,22 +491,9 @@ protected function _assertValidExtend($extendingSection, $extendedSection) protected function _arrayMergeRecursive($firstArray, $secondArray) { if (is_array($firstArray) && is_array($secondArray)) { - foreach ($secondArray as $key => $value) { - if (isset($firstArray[$key])) { - $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value); - } else { - if($key === 0) { - $firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value)); - } else { - $firstArray[$key] = $value; - } - } - } - } else { - $firstArray = $secondArray; + return array_replace_recursive($firstArray, $secondArray); } - - return $firstArray; + return $secondArray; } /** diff --git a/src/Ini.php b/src/Ini.php index d2ff389..c59abf2 100644 --- a/src/Ini.php +++ b/src/Ini.php @@ -261,6 +261,13 @@ protected function _processKey($config, $key, $value) throw new Exception\RuntimeException("Invalid key '$key'"); } } else { + if (is_string($value)) { + if(strtolower(trim($value)) === 'true') { + $value = '1'; + } elseif(strtolower(trim($value)) === 'false') { + $value = ''; + } + } $config[$key] = $value; } return $config; diff --git a/src/Xml.php b/src/Xml.php index 26046cf..48dddac 100644 --- a/src/Xml.php +++ b/src/Xml.php @@ -292,4 +292,33 @@ protected function _toArray(\SimpleXMLElement $xmlObject) return $config; } + + /** + * Merge two arrays recursively, overwriting keys of the same name + * in $firstArray with the value in $secondArray. + * + * @param mixed $firstArray First array + * @param mixed $secondArray Second array to merge into first array + * @return array + */ + protected function _arrayMergeRecursive($firstArray, $secondArray) + { + if (is_array($firstArray) && is_array($secondArray)) { + foreach ($secondArray as $key => $value) { + if (isset($firstArray[$key])) { + $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value); + } else { + if($key === 0) { + $firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value)); + } else { + $firstArray[$key] = $value; + } + } + } + } else { + $firstArray = $secondArray; + } + + return $firstArray; + } } 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 diff --git a/test/IniTest.php b/test/IniTest.php index a0ccfc9..1dbc363 100644 --- a/test/IniTest.php +++ b/test/IniTest.php @@ -47,6 +47,7 @@ public function setUp() $this->_nonReadableConfig = __DIR__ . '/_files/nonreadable.ini'; $this->_iniFileNoSectionsConfig = __DIR__ . '/_files/nosections.ini'; $this->_iniFileInvalid = __DIR__ . '/_files/invalid.ini'; + $this->_iniFileBooleans = __DIR__ . '/_files/booleans.ini'; } public function testLoadSingleSection() @@ -317,4 +318,13 @@ public function testPreservationOfIntegerKeys() } + public function testBooleans() + { + $config = new Ini($this->_iniFileBooleans, 'all'); + $this->assertEquals(true, (bool)$config->trueValue); + $this->assertEquals(false, (bool)$config->falseValue); + $this->assertEquals(true, (bool)$config->trueString); + $this->assertEquals(false, (bool)$config->falseString); + } + } diff --git a/test/_files/booleans.ini b/test/_files/booleans.ini new file mode 100644 index 0000000..a4490b5 --- /dev/null +++ b/test/_files/booleans.ini @@ -0,0 +1,6 @@ +[all] +trueValue = true +falseValue = false +trueString = "true" +falseString = "false" +