Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 16 deletions.
68 changes: 52 additions & 16 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions src/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 19 additions & 0 deletions test/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/IniTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}

}
6 changes: 6 additions & 0 deletions test/_files/booleans.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[all]
trueValue = true
falseValue = false
trueString = "true"
falseString = "false"

0 comments on commit c74649b

Please sign in to comment.