diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index cef1c6e7f5..17f62bcc45 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -70,7 +70,6 @@ class PropertyAccessor implements PropertyAccessorInterface } $propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1); - $overwrite = true; // Add the root object to the list array_unshift($propertyValues, array( @@ -81,18 +80,19 @@ class PropertyAccessor implements PropertyAccessorInterface for ($i = count($propertyValues) - 1; $i >= 0; --$i) { $objectOrArray = & $propertyValues[$i][self::VALUE]; - if ($overwrite) { - $property = $propertyPath->getElement($i); + $property = $propertyPath->getElement($i); - if ($propertyPath->isIndex($i)) { - $this->writeIndex($objectOrArray, $property, $value); - } else { - $this->writeProperty($objectOrArray, $property, $value); - } + if ($propertyPath->isIndex($i)) { + $this->writeIndex($objectOrArray, $property, $value); + } else { + $this->writeProperty($objectOrArray, $property, $value); + } + + if ($propertyValues[$i][self::IS_REF]) { + return; } $value = & $objectOrArray; - $overwrite = !$propertyValues[$i][self::IS_REF]; } } @@ -127,7 +127,6 @@ class PropertyAccessor implements PropertyAccessorInterface try { $propertyValues = $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1); - $overwrite = true; // Add the root object to the list array_unshift($propertyValues, array( @@ -138,21 +137,21 @@ class PropertyAccessor implements PropertyAccessorInterface for ($i = count($propertyValues) - 1; $i >= 0; --$i) { $objectOrArray = $propertyValues[$i][self::VALUE]; - if ($overwrite) { - $property = $propertyPath->getElement($i); + $property = $propertyPath->getElement($i); - if ($propertyPath->isIndex($i)) { - if (!$objectOrArray instanceof \ArrayAccess && !is_array($objectOrArray)) { - return false; - } - } else { - if (!$this->isPropertyWritable($objectOrArray, $property)) { - return false; - } + if ($propertyPath->isIndex($i)) { + if (!$objectOrArray instanceof \ArrayAccess && !is_array($objectOrArray)) { + return false; + } + } else { + if (!$this->isPropertyWritable($objectOrArray, $property)) { + return false; } } - $overwrite = !$propertyValues[$i][self::IS_REF]; + if ($propertyValues[$i][self::IS_REF]) { + return true; + } } return true; diff --git a/src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php b/src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php index 5cd605b164..7b1b927529 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php +++ b/src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php @@ -25,6 +25,7 @@ class TestClass private $publicAccessorWithMoreRequiredParameters; private $publicIsAccessor; private $publicHasAccessor; + private $publicGetter; public function __construct($value) { @@ -37,6 +38,7 @@ class TestClass $this->publicAccessorWithMoreRequiredParameters = $value; $this->publicIsAccessor = $value; $this->publicHasAccessor = $value; + $this->publicGetter = $value; } public function setPublicAccessor($value) @@ -166,4 +168,9 @@ class TestClass { return 'foobar'; } + + public function getPublicGetter() + { + return $this->publicGetter; + } } diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php index 58d4d74402..0c46467567 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php @@ -419,6 +419,14 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase array(array('index' => array('%!@$§.' => 'Bernhard')), '[index][%!@$§.]', 'Bernhard'), array((object) array('%!@$§' => 'Bernhard'), '%!@$§', 'Bernhard'), array((object) array('property' => (object) array('%!@$§' => 'Bernhard')), 'property.%!@$§', 'Bernhard'), + + // nested objects and arrays + array(array('foo' => new TestClass('bar')), '[foo].publicGetSetter', 'bar'), + array(new TestClass(array('foo' => 'bar')), 'publicGetSetter[foo]', 'bar'), + array(new TestClass(new TestClass('bar')), 'publicGetter.publicGetSetter', 'bar'), + array(new TestClass(array('foo' => new TestClass('bar'))), 'publicGetter[foo].publicGetSetter', 'bar'), + array(new TestClass(new TestClass(new TestClass('bar'))), 'publicGetter.publicGetter.publicGetSetter', 'bar'), + array(new TestClass(array('foo' => array('baz' => new TestClass('bar')))), 'publicGetter[foo][baz].publicGetSetter', 'bar'), ); }