Skip to content

Commit

Permalink
bug #13346 [PropertyAccessor] Allow null value for a array (2.3) (boe…
Browse files Browse the repository at this point in the history
…kkooi)

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 | #12482
| License       | MIT
| Doc PR        | -

Original PR is #12511
A rebase on 2.3 was requested by @fabpot

Commits
-------

9706b09 [PropertyAccessor] Added test to allow null value for a array
  • Loading branch information
fabpot committed Jan 9, 2015
2 parents d5e9de2 + 9706b09 commit 55b8939
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr

$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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ public function testGetValueThrowsExceptionIfEmpty()
$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();
Expand Down

0 comments on commit 55b8939

Please sign in to comment.