From 9706b090c08a472428873d8ab6a9475705da0266 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Fri, 9 Jan 2015 15:53:40 +0100 Subject: [PATCH] [PropertyAccessor] Added test to allow null value for a array --- .../Component/PropertyAccess/PropertyAccessor.php | 9 +++++++-- .../PropertyAccess/Tests/PropertyAccessorTest.php | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index 252ad6896d..92caf1e826 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -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; } diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php index 572f123bd9..ca49be6012 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php @@ -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();