Skip to content

Commit 55b8939

Browse files
committed
bug symfony#13346 [PropertyAccessor] Allow null value for a array (2.3) (boekkooi)
This PR was merged into the 2.3 branch. Discussion ---------- [PropertyAccessor] Allow null value for a array (2.3) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#12482 | License | MIT | Doc PR | - Original PR is symfony#12511 A rebase on 2.3 was requested by @fabpot Commits ------- 9706b09 [PropertyAccessor] Added test to allow null value for a array
2 parents d5e9de2 + 9706b09 commit 55b8939

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,15 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr
117117

118118
$property = $propertyPath->getElement($i);
119119
$isIndex = $propertyPath->isIndex($i);
120-
$isArrayAccess = is_array($objectOrArray) || $objectOrArray instanceof \ArrayAccess;
121120

122121
// Create missing nested arrays on demand
123-
if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) {
122+
if (
123+
$isIndex &&
124+
(
125+
($objectOrArray instanceof \ArrayAccess && !isset($objectOrArray[$property])) ||
126+
(is_array($objectOrArray) && !array_key_exists($property, $objectOrArray))
127+
)
128+
) {
124129
$objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
125130
}
126131

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ public function testGetValueThrowsExceptionIfEmpty()
221221
$this->propertyAccessor->getValue('', 'foobar');
222222
}
223223

224+
public function testGetValueWhenArrayValueIsNull()
225+
{
226+
$this->propertyAccessor = new PropertyAccessor(false, true);
227+
$this->assertNull($this->propertyAccessor->getValue(array('index' => array('nullable' => null)), '[index][nullable]'));
228+
}
229+
224230
public function testSetValueUpdatesArrays()
225231
{
226232
$array = array();

0 commit comments

Comments
 (0)