8000 [PropertyAccess] stop overwriting once a reference is reached (3rd) · symfony/symfony@d733a88 · GitHub
[go: up one dir, main page]

Skip to content

Commit d733a88

Browse files
bananerTobion
authored andcommitted
[PropertyAccess] stop overwriting once a reference is reached (3rd)
1 parent 38a5ebd commit d733a88

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
7070
}
7171

7272
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
73-
$overwrite = true;
7473

7574
// Add the root object to the list
7675
array_unshift($propertyValues, array(
@@ -81,18 +80,19 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
8180
for ($i = count($propertyValues) - 1; $i >= 0; --$i) {
8281
$objectOrArray = & $propertyValues[$i][self::VALUE];
8382

84-
if ($overwrite) {
85-
$property = $propertyPath->getElement($i);
83+
$property = $propertyPath->getElement($i);
8684

87-
if ($propertyPath->isIndex($i)) {
88-
$this->writeIndex($objectOrArray, $property, $value);
89-
} else {
90-
$this->writeProperty($objectOrArray, $property, $value);
91-
}
85+
if ($propertyPath->isIndex($i)) {
86+
$this->writeIndex($objectOrArray, $property, $value);
87+
} else {
88+
$this->writeProperty($objectOrArray, $property, $value);
89+
}
90+
91+
if ($propertyValues[$i][self::IS_REF]) {
92+
return;
9293
}
9394

9495
$value = & $objectOrArray;
95-
$overwrite = !$propertyValues[$i][self::IS_REF];
9696
}
9797
}
9898

@@ -127,7 +127,6 @@ public function isWritable($objectOrArray, $propertyPath)
127127

128128
try {
129129
$propertyValues = $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
130-
$overwrite = true;
131130

132131
// Add the root object to the list
133132
array_unshift($propertyValues, array(
@@ -138,21 +137,21 @@ public function isWritable($objectOrArray, $propertyPath)
138137
for ($i = count($propertyValues) - 1; $i >= 0; --$i) {
139138
$objectOrArray = $propertyValues[$i][self::VALUE];
140139

141-
if ($overwrite) {
142-
$property = $propertyPath->getElement($i);
140+
$property = $propertyPath->getElement($i);
143141

144-
if ($propertyPath->isIndex($i)) {
145-
if (!$objectOrArray instanceof \ArrayAccess && !is_array($objectOrArray)) {
146-
return false;
147-
}
148-
} else {
149-
if (!$this->isPropertyWritable($objectOrArray, $property)) {
150-
return false;
151-
}
142+
if ($propertyPath->isIndex($i)) {
143+
if (!$objectOrArray instanceof \ArrayAccess && !is_array($objectOrArray)) {
144+
return false;
145+
}
146+
} else {
147+
if (!$this->isPropertyWritable($objectOrArray, $property)) {
148+
return false;
152149
}
153150
}
154151

155-
$overwrite = !$propertyValues[$i][self::IS_REF];
152+
if ($propertyValues[$i][self::IS_REF]) {
153+
return true;
154+
}
156155
}
157156

158157
return true;

src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestClass
2525
private $publicAccessorWithMoreRequiredParameters;
2626
private $publicIsAccessor;
2727
private $publicHasAccessor;
28+
private $publicGetter;
2829

2930
public function __construct($value)
3031
{
@@ -37,6 +38,7 @@ public function __construct($value)
3738
$this->publicAccessorWithMoreRequiredParameters = $value;
3839
$this->publicIsAccessor = $value;
3940
$this->publicHasAccessor = $value;
41+
$this->publicGetter = $value;
4042
}
4143

4244
public function setPublicAccessor($value)
@@ -166,4 +168,9 @@ private function hasPrivateHasAccessor()
166168
{
167169
return 'foobar';
168170
}
171+
172+
public function getPublicGetter()
173+
{
174+
return $this->publicGetter;
175+
}
169176
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@ public function getValidPropertyPaths()
419419
array(array('index' => array('%!@$§.' => 'Bernhard')), '[index][%!@$§.]', 'Bernhard'),
420420
array((object) array('%!@$§' => 'Bernhard'), '%!@$§', 'Bernhard'),
421421
array((object) array('property' => (object) array('%!@$§' => 'Bernhard')), 'property.%!@$§', 'Bernhard'),
422+
423+
// nested objects and arrays
424+
array(array('foo' => new TestClass('bar')), '[foo].publicGetSetter', 'bar'),
425+
array(new TestClass(array('foo' => 'bar')), 'publicGetSetter[foo]', 'bar'),
426+
array(new TestClass(new TestClass('bar')), 'publicGetter.publicGetSetter', 'bar'),
427+
array(new TestClass(array('foo' => new TestClass('bar'))), 'publicGetter[foo].publicGetSetter', 'bar'),
428+
array(new TestClass(new TestClass(new TestClass('bar'))), 'publicGetter.publicGetter.publicGetSetter', 'bar'),
429+
array(new TestClass(array('foo' => array('baz' => new TestClass('bar')))), 'publicGetter[foo][baz].publicGetSetter', 'bar'),
422430
);
423431
}
424432

0 commit comments

Comments
 (0)
0