Skip to content

Commit c210fab

Browse files
bug symfony#58110 [PropertyAccess] Fix handling property names with a . (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [PropertyAccess] Fix handling property names with a `.` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#58100 | License | MIT If the property path contains a dot, it is considered to be an access to an underlying property. However, some edge cases allow to have dots in property names, especially with `stdClass`. Minimal reproducer: ```php $stdclass = (object) ['bankAccount.iban' => 'NL16TEST0436169118', 'bankSummary' => '']; $accessor = PropertyAccess::createPropertyAccessor(); dump($accessor->getValue($stdclass, 'bankAccount.iban')); // returns "NL16TEST0436169118" $accessor->setValue($stdclass, 'bankAccount.iban', 'value'); dump($accessor->getValue($stdclass, 'bankAccount.iban')); // returns "value" ``` Commits ------- d939a16 [PropertyAccess] Fix handling property names with a `.`
2 parents 78c0da1 + d939a16 commit c210fab

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function getValue($objectOrArray, $propertyPath)
150150
self::VALUE => $objectOrArray,
151151
];
152152

153-
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
153+
if (\is_object($objectOrArray) && (false === strpbrk((string) $propertyPath, '.[') || $objectOrArray instanceof \stdClass && property_exists($objectOrArray, $propertyPath))) {
154154
return $this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty)[self::VALUE];
155155
}
156156

@@ -166,7 +166,7 @@ public function getValue($objectOrArray, $propertyPath)
166166
*/
167167
public function setValue(&$objectOrArray, $propertyPath, $value)
168168
{
169-
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
169+
if (\is_object($objectOrArray) && (false === strpbrk((string) $propertyPath, '.[') || $objectOrArray instanceof \stdClass && property_exists($objectOrArray, $propertyPath))) {
170170
$zval = [
171171
self::VALUE => $objectOrArray,
172172
];
@@ -293,7 +293,13 @@ public function isReadable($objectOrArray, $propertyPath)
293293
$zval = [
294294
self::VALUE => $objectOrArray,
295295
];
296-
$this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
296+
297+
// handle stdClass with properties with a dot in the name
298+
if ($objectOrArray instanceof \stdClass && str_contains($propertyPath, '.') && property_exists($objectOrArray, $propertyPath)) {
299+
$this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty);
300+
} else {
301+
$this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
302+
}
297303

298304
return true;
299305
} catch (AccessException $e) {
@@ -314,6 +320,14 @@ public function isWritable($objectOrArray, $propertyPath)
314320
$zval = [
315321
self::VALUE => $objectOrArray,
316322
];
323+
324+
// handle stdClass with properties with a dot in the name
325+
if ($objectOrArray instanceof \stdClass && str_contains($propertyPath, '.') && property_exists($objectOrArray, $propertyPath)) {
326+
$this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty);
327+
328+
return true;
329+
}
330+
317331
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength() - 1);
318332

319333
for ($i = \count($propertyValues) - 1; 0 <= $i; --$i) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ public static function getValidPropertyPaths()
674674
[['firstName' => 'Bernhard'], '[firstName]', 'Bernhard'],
675675
[['index' => ['firstName' => 'Bernhard']], '[index][firstName]', 'Bernhard'],
676676
[(object) ['firstName' => 'Bernhard'], 'firstName', 'Bernhard'],
677+
[(object) ['first.Name' => 'Bernhard'], 'first.Name', 'Bernhard'],
677678
[(object) ['property' => ['firstName' => 'Bernhard']], 'property[firstName]', 'Bernhard'],
678679
[['index' => (object) ['firstName' => 'Bernhard']], '[index].firstName', 'Bernhard'],
679680
[(object) ['property' => (object) ['firstName' => 'Bernhard']], 'property.firstName', 'Bernhard'],

0 commit comments

Comments
 (0)