speed up accessing object properties

This commit is contained in:
Christian Flothmann 2019-01-27 11:32:59 +01:00
parent 1aa652e2cb
commit ef7876e517
1 changed files with 23 additions and 2 deletions

View File

@ -82,11 +82,16 @@ class PropertyAccessor implements PropertyAccessorInterface
*/
public function getValue($objectOrArray, $propertyPath)
{
$propertyPath = $this->getPropertyPath($propertyPath);
$zval = [
self::VALUE => $objectOrArray,
];
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
return $this->readProperty($zval, $propertyPath)[self::VALUE];
}
$propertyPath = $this->getPropertyPath($propertyPath);
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
return $propertyValues[\count($propertyValues) - 1][self::VALUE];
@ -97,6 +102,22 @@ class PropertyAccessor implements PropertyAccessorInterface
*/
public function setValue(&$objectOrArray, $propertyPath, $value)
{
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
$zval = [
self::VALUE => $objectOrArray,
];
try {
$this->writeProperty($zval, $propertyPath, $value);
return;
} catch (\TypeError $e) {
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0, $propertyPath);
// It wasn't thrown in this class so rethrow it
throw $e;
}
}
$propertyPath = $this->getPropertyPath($propertyPath);
$zval = [