[PropertyAccessor] Added test to allow null value for a array

This commit is contained in:
Warnar Boekkooi 2015-01-09 15:53:40 +01:00
parent d5e9de2f94
commit 9706b090c0
2 changed files with 13 additions and 2 deletions

View File

@ -117,10 +117,15 @@ class PropertyAccessor implements PropertyAccessorInterface
$property = $propertyPath->getElement($i);
$isIndex = $propertyPath->isIndex($i);
$isArrayAccess = is_array($objectOrArray) || $objectOrArray instanceof \ArrayAccess;
// Create missing nested arrays on demand
if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) {
if (
$isIndex &&
(
($objectOrArray instanceof \ArrayAccess && !isset($objectOrArray[$property])) ||
(is_array($objectOrArray) && !array_key_exists($property, $objectOrArray))
)
) {
$objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
}

View File

@ -221,6 +221,12 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase
$this->propertyAccessor->getValue('', 'foobar');
}
public function testGetValueWhenArrayValueIsNull()
{
$this->propertyAccessor = new PropertyAccessor(false, true);
$this->assertNull($this->propertyAccessor->getValue(array('index' => array('nullable' => null)), '[index][nullable]'));
}
public function testSetValueUpdatesArrays()
{
$array = array();