8000 [PropertyAccess] Fix handling property names with a `.` · symfony/symfony@c0e415c · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c0e415c

Browse files
[PropertyAccess] Fix handling property names with a .
1 parent 34994f1 commit c0e415c

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, '.[') || property_exists($ 10000 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, '.[') || 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)
0