Skip to content

Commit

Permalink
[PropertyAccess] Fix handling property names with a .
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Aug 29, 2024
1 parent 6e48341 commit 2d75186
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 17 additions & 3 deletions PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function getValue($objectOrArray, $propertyPath)
self::VALUE => $objectOrArray,
];

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

Expand All @@ -166,7 +166,7 @@ public function getValue($objectOrArray, $propertyPath)
*/
public function setValue(&$objectOrArray, $propertyPath, $value)
{
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
if (\is_object($objectOrArray) && (false === strpbrk((string) $propertyPath, '.[') || $objectOrArray instanceof \stdClass && property_exists($objectOrArray, $propertyPath))) {
$zval = [
self::VALUE => $objectOrArray,
];
Expand Down Expand Up @@ -293,7 +293,13 @@ public function isReadable($objectOrArray, $propertyPath)
$zval = [
self::VALUE => $objectOrArray,
];
$this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);

// handle stdClass with properties with a dot in the name
if ($objectOrArray instanceof \stdClass && str_contains($propertyPath, '.') && property_exists($objectOrArray, $propertyPath)) {
$this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty);
} else {
$this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
}

return true;
} catch (AccessException $e) {
Expand All @@ -314,6 +320,14 @@ public function isWritable($objectOrArray, $propertyPath)
$zval = [
self::VALUE => $objectOrArray,
];

// handle stdClass with properties with a dot in the name
if ($objectOrArray instanceof \stdClass && str_contains($propertyPath, '.') && property_exists($objectOrArray, $propertyPath)) {
$this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty);

return true;
}

$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength() - 1);

for ($i = \count($propertyValues) - 1; 0 <= $i; --$i) {
Expand Down
1 change: 1 addition & 0 deletions Tests/PropertyAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ public static function getValidPropertyPaths()
[['firstName' => 'Bernhard'], '[firstName]', 'Bernhard'],
[['index' => ['firstName' => 'Bernhard']], '[index][firstName]', 'Bernhard'],
[(object) ['firstName' => 'Bernhard'], 'firstName', 'Bernhard'],
[(object) ['first.Name' => 'Bernhard'], 'first.Name', 'Bernhard'],
[(object) ['property' => ['firstName' => 'Bernhard']], 'property[firstName]', 'Bernhard'],
[['index' => (object) ['firstName' => 'Bernhard']], '[index].firstName', 'Bernhard'],
[(object) ['property' => (object) ['firstName' => 'Bernhard']], 'property.firstName', 'Bernhard'],
Expand Down

0 comments on commit 2d75186

Please sign in to comment.