Skip to content

Commit

Permalink
Merge branch '5.4' into 6.4
Browse files Browse the repository at this point in the history
* 5.4:
  synchronize IBAN formats
  [PropertyAccess] Fix handling property names with a `.`
  • Loading branch information
nicolas-grekas committed Aug 30, 2024
2 parents e4d9b00 + 2d75186 commit d60a85b
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 @@ -95,7 +95,7 @@ public function getValue(object|array $objectOrArray, string|PropertyPathInterfa
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 @@ -111,7 +111,7 @@ public function getValue(object|array $objectOrArray, string|PropertyPathInterfa
*/
public function setValue(object|array &$objectOrArray, string|PropertyPathInterface $propertyPath, mixed $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 @@ -214,7 +214,13 @@ public function isReadable(object|array $objectOrArray, string|PropertyPathInter
$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) {
Expand All @@ -232,6 +238,14 @@ public function isWritable(object|array $objectOrArray, string|PropertyPathInter
$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 @@ -526,6 +526,7 @@ public static function getValidWritePropertyPaths()
[['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 d60a85b

Please sign in to comment.