login consent app sql

This commit is contained in:
2022-05-03 08:54:45 +02:00
parent e7253acfd8
commit f9a6535906
1652 changed files with 187600 additions and 45 deletions

View File

@ -0,0 +1,70 @@
CHANGELOG
=========
5.3.0
-----
* deprecate passing a boolean as the second argument of `PropertyAccessor::__construct()`, expecting a combination of bitwise flags instead
5.2.0
-----
* deprecated passing a boolean as the first argument of `PropertyAccessor::__construct()`, expecting a combination of bitwise flags instead
* added the ability to disable usage of the magic `__get` & `__set` methods
5.1.0
-----
* Added an `UninitializedPropertyException`
* Linking to PropertyInfo extractor to remove a lot of duplicate code
4.4.0
-----
* deprecated passing `null` as `$defaultLifetime` 2nd argument of `PropertyAccessor::createCache()` method,
pass `0` instead
4.3.0
-----
* added a `$throwExceptionOnInvalidPropertyPath` argument to the PropertyAccessor constructor.
* added `enableExceptionOnInvalidPropertyPath()`, `disableExceptionOnInvalidPropertyPath()` and
`isExceptionOnInvalidPropertyPath()` methods to `PropertyAccessorBuilder`
4.0.0
-----
* removed the `StringUtil` class, use `Symfony\Component\Inflector\Inflector`
3.1.0
-----
* deprecated the `StringUtil` class, use `Symfony\Component\Inflector\Inflector`
instead
2.7.0
------
* `UnexpectedTypeException` now expects three constructor arguments: The invalid property value,
the `PropertyPathInterface` object and the current index of the property path.
2.5.0
------
* allowed non alpha numeric characters in second level and deeper object properties names
* [BC BREAK] when accessing an index on an object that does not implement
ArrayAccess, a NoSuchIndexException is now thrown instead of the
semantically wrong NoSuchPropertyException
* [BC BREAK] added isReadable() and isWritable() to PropertyAccessorInterface
2.3.0
------
* added PropertyAccessorBuilder, to enable or disable the support of "__call"
* added support for "__call" in the PropertyAccessor (disabled by default)
* [BC BREAK] changed PropertyAccessor to continue its search for a property or
method even if a non-public match was found. Before, a PropertyAccessDeniedException
was thrown in this case. Class PropertyAccessDeniedException was removed
now.
* deprecated PropertyAccess::getPropertyAccessor
* added PropertyAccess::createPropertyAccessor and PropertyAccess::createPropertyAccessorBuilder

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Thrown when a property path is not available.
*
* @author Stéphane Escandell <stephane.escandell@gmail.com>
*/
class AccessException extends RuntimeException
{
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Marker interface for the PropertyAccess component.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface ExceptionInterface extends \Throwable
{
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Base InvalidArgumentException for the PropertyAccess component.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Thrown when a property path is malformed.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class InvalidPropertyPathException extends RuntimeException
{
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Thrown when an index cannot be found.
*
* @author Stéphane Escandell <stephane.escandell@gmail.com>
*/
class NoSuchIndexException extends AccessException
{
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Thrown when a property cannot be found.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class NoSuchPropertyException extends AccessException
{
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Base OutOfBoundsException for the PropertyAccess component.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class OutOfBoundsException extends \OutOfBoundsException implements ExceptionInterface
{
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Base RuntimeException for the PropertyAccess component.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}

View File

@ -0,0 +1,39 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
use Symfony\Component\PropertyAccess\PropertyPathInterface;
/**
* Thrown when a value does not match an expected type.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UnexpectedTypeException extends RuntimeException
{
/**
* @param mixed $value The unexpected value found while traversing property path
* @param int $pathIndex The property path index when the unexpected value was found
*/
public function __construct($value, PropertyPathInterface $path, int $pathIndex)
{
$message = sprintf(
'PropertyAccessor requires a graph of objects or arrays to operate on, '.
'but it found type "%s" while trying to traverse path "%s" at property "%s".',
\gettype($value),
(string) $path,
$path->getElement($pathIndex)
);
parent::__construct($message);
}
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Exception;
/**
* Thrown when a property is not initialized.
*
* @author Jules Pietri <jules@heahprod.com>
*/
class UninitializedPropertyException extends AccessException
{
}

19
vendor/symfony/property-access/LICENSE vendored Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2004-2022 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
/**
* Entry point of the PropertyAccess component.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
final class PropertyAccess
{
/**
* Creates a property accessor with the default configuration.
*/
public static function createPropertyAccessor(): PropertyAccessor
{
return self::createPropertyAccessorBuilder()->getPropertyAccessor();
}
public static function createPropertyAccessorBuilder(): PropertyAccessorBuilder
{
return new PropertyAccessorBuilder();
}
/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}

View File

@ -0,0 +1,748 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyReadInfo;
use Symfony\Component\PropertyInfo\PropertyReadInfoExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyWriteInfo;
use Symfony\Component\PropertyInfo\PropertyWriteInfoExtractorInterface;
/**
* Default implementation of {@link PropertyAccessorInterface}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
class PropertyAccessor implements PropertyAccessorInterface
{
/** @var int Allow none of the magic methods */
public const DISALLOW_MAGIC_METHODS = ReflectionExtractor::DISALLOW_MAGIC_METHODS;
/** @var int Allow magic __get methods */
public const MAGIC_GET = ReflectionExtractor::ALLOW_MAGIC_GET;
/** @var int Allow magic __set methods */
public const MAGIC_SET = ReflectionExtractor::ALLOW_MAGIC_SET;
/** @var int Allow magic __call methods */
public const MAGIC_CALL = ReflectionExtractor::ALLOW_MAGIC_CALL;
public const DO_NOT_THROW = 0;
public const THROW_ON_INVALID_INDEX = 1;
public const THROW_ON_INVALID_PROPERTY_PATH = 2;
private const VALUE = 0;
private const REF = 1;
private const IS_REF_CHAINED = 2;
private const CACHE_PREFIX_READ = 'r';
private const CACHE_PREFIX_WRITE = 'w';
private const CACHE_PREFIX_PROPERTY_PATH = 'p';
private $magicMethodsFlags;
private $ignoreInvalidIndices;
private $ignoreInvalidProperty;
/**
* @var CacheItemPoolInterface
*/
private $cacheItemPool;
private $propertyPathCache = [];
/**
* @var PropertyReadInfoExtractorInterface
*/
private $readInfoExtractor;
/**
* @var PropertyWriteInfoExtractorInterface
*/
private $writeInfoExtractor;
private $readPropertyCache = [];
private $writePropertyCache = [];
private const RESULT_PROTO = [self::VALUE => null];
/**
* Should not be used by application code. Use
* {@link PropertyAccess::createPropertyAccessor()} instead.
*
* @param int $magicMethods A bitwise combination of the MAGIC_* constants
* to specify the allowed magic methods (__get, __set, __call)
* or self::DISALLOW_MAGIC_METHODS for none
* @param int $throw A bitwise combination of the THROW_* constants
* to specify when exceptions should be thrown
* @param PropertyReadInfoExtractorInterface $readInfoExtractor
* @param PropertyWriteInfoExtractorInterface $writeInfoExtractor
*/
public function __construct($magicMethods = self::MAGIC_GET | self::MAGIC_SET, $throw = self::THROW_ON_INVALID_PROPERTY_PATH, CacheItemPoolInterface $cacheItemPool = null, $readInfoExtractor = null, $writeInfoExtractor = null)
{
if (\is_bool($magicMethods)) {
trigger_deprecation('symfony/property-access', '5.2', 'Passing a boolean as the first argument to "%s()" is deprecated. Pass a combination of bitwise flags instead (i.e an integer).', __METHOD__);
$magicMethods = ($magicMethods ? self::MAGIC_CALL : 0) | self::MAGIC_GET | self::MAGIC_SET;
} elseif (!\is_int($magicMethods)) {
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be an integer, "%s" given.', __METHOD__, get_debug_type($readInfoExtractor)));
}
if (\is_bool($throw)) {
trigger_deprecation('symfony/property-access', '5.3', 'Passing a boolean as the second argument to "%s()" is deprecated. Pass a combination of bitwise flags instead (i.e an integer).', __METHOD__);
$throw = $throw ? self::THROW_ON_INVALID_INDEX : self::DO_NOT_THROW;
if (!\is_bool($readInfoExtractor)) {
$throw |= self::THROW_ON_INVALID_PROPERTY_PATH;
}
}
if (\is_bool($readInfoExtractor)) {
trigger_deprecation('symfony/property-access', '5.3', 'Passing a boolean as the fourth argument to "%s()" is deprecated. Pass a combination of bitwise flags as the second argument instead (i.e an integer).', __METHOD__);
if ($readInfoExtractor) {
$throw |= self::THROW_ON_INVALID_PROPERTY_PATH;
}
$readInfoExtractor = $writeInfoExtractor;
$writeInfoExtractor = 4 < \func_num_args() ? func_get_arg(4) : null;
}
if (null !== $readInfoExtractor && !$readInfoExtractor instanceof PropertyReadInfoExtractorInterface) {
throw new \TypeError(sprintf('Argument 4 passed to "%s()" must be null or an instance of "%s", "%s" given.', __METHOD__, PropertyReadInfoExtractorInterface::class, get_debug_type($readInfoExtractor)));
}
if (null !== $writeInfoExtractor && !$writeInfoExtractor instanceof PropertyWriteInfoExtractorInterface) {
throw new \TypeError(sprintf('Argument 5 passed to "%s()" must be null or an instance of "%s", "%s" given.', __METHOD__, PropertyWriteInfoExtractorInterface::class, get_debug_type($writeInfoExtractor)));
}
$this->magicMethodsFlags = $magicMethods;
$this->ignoreInvalidIndices = 0 === ($throw & self::THROW_ON_INVALID_INDEX);
$this->cacheItemPool = $cacheItemPool instanceof NullAdapter ? null : $cacheItemPool; // Replace the NullAdapter by the null value
$this->ignoreInvalidProperty = 0 === ($throw & self::THROW_ON_INVALID_PROPERTY_PATH);
$this->readInfoExtractor = $readInfoExtractor ?? new ReflectionExtractor([], null, null, false);
$this->writeInfoExtractor = $writeInfoExtractor ?? new ReflectionExtractor(['set'], null, null, false);
}
/**
* {@inheritdoc}
*/
public function getValue($objectOrArray, $propertyPath)
{
$zval = [
self::VALUE => $objectOrArray,
];
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
return $this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty)[self::VALUE];
}
$propertyPath = $this->getPropertyPath($propertyPath);
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
return $propertyValues[\count($propertyValues) - 1][self::VALUE];
}
/**
* {@inheritdoc}
*/
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, $e);
// It wasn't thrown in this class so rethrow it
throw $e;
}
}
$propertyPath = $this->getPropertyPath($propertyPath);
$zval = [
self::VALUE => $objectOrArray,
self::REF => &$objectOrArray,
];
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength() - 1);
$overwrite = true;
try {
for ($i = \count($propertyValues) - 1; 0 <= $i; --$i) {
$zval = $propertyValues[$i];
unset($propertyValues[$i]);
// You only need set value for current element if:
// 1. it's the parent of the last index element
// OR
// 2. its child is not passed by reference
//
// This may avoid uncessary value setting process for array elements.
// For example:
// '[a][b][c]' => 'old-value'
// If you want to change its value to 'new-value',
// you only need set value for '[a][b][c]' and it's safe to ignore '[a][b]' and '[a]'
if ($overwrite) {
$property = $propertyPath->getElement($i);
if ($propertyPath->isIndex($i)) {
if ($overwrite = !isset($zval[self::REF])) {
$ref = &$zval[self::REF];
$ref = $zval[self::VALUE];
}
$this->writeIndex($zval, $property, $value);
if ($overwrite) {
$zval[self::VALUE] = $zval[self::REF];
}
} else {
$this->writeProperty($zval, $property, $value);
}
// if current element is an object
// OR
// if current element's reference chain is not broken - current element
// as well as all its ancients in the property path are all passed by reference,
// then there is no need to continue the value setting process
if (\is_object($zval[self::VALUE]) || isset($zval[self::IS_REF_CHAINED])) {
break;
}
}
$value = $zval[self::VALUE];
}
} catch (\TypeError $e) {
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0, $propertyPath, $e);
// It wasn't thrown in this class so rethrow it
throw $e;
}
}
private static function throwInvalidArgumentException(string $message, array $trace, int $i, string $propertyPath, \Throwable $previous = null): void
{
if (!isset($trace[$i]['file']) || __FILE__ !== $trace[$i]['file']) {
return;
}
if (\PHP_VERSION_ID < 80000) {
if (preg_match('/^Typed property \S+::\$\S+ must be (\S+), (\S+) used$/', $message, $matches)) {
[, $expectedType, $actualType] = $matches;
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
}
if (!str_starts_with($message, 'Argument ')) {
return;
}
$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
$pos += \strlen($delim);
$j = strpos($message, ',', $pos);
$type = substr($message, 2 + $j, strpos($message, ' given', $j) - $j - 2);
$message = substr($message, $pos, $j - $pos);
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $message, 'NULL' === $type ? 'null' : $type, $propertyPath), 0, $previous);
}
if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
[, $expectedType, $actualType] = $matches;
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
}
if (preg_match('/^Cannot assign (\S+) to property \S+::\$\S+ of type (\S+)$/', $message, $matches)) {
[, $actualType, $expectedType] = $matches;
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
}
}
/**
* {@inheritdoc}
*/
public function isReadable($objectOrArray, $propertyPath)
{
if (!$propertyPath instanceof PropertyPathInterface) {
$propertyPath = new PropertyPath($propertyPath);
}
try {
$zval = [
self::VALUE => $objectOrArray,
];
$this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
return true;
} catch (AccessException $e) {
return false;
} catch (UnexpectedTypeException $e) {
return false;
}
}
/**
* {@inheritdoc}
*/
public function isWritable($objectOrArray, $propertyPath)
{
$propertyPath = $this->getPropertyPath($propertyPath);
try {
$zval = [
self::VALUE => $objectOrArray,
];
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength() - 1);
for ($i = \count($propertyValues) - 1; 0 <= $i; --$i) {
$zval = $propertyValues[$i];
unset($propertyValues[$i]);
if ($propertyPath->isIndex($i)) {
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
return false;
}
} elseif (!\is_object($zval[self::VALUE]) || !$this->isPropertyWritable($zval[self::VALUE], $propertyPath->getElement($i))) {
return false;
}
if (\is_object($zval[self::VALUE])) {
return true;
}
}
return true;
} catch (AccessException $e) {
return false;
} catch (UnexpectedTypeException $e) {
return false;
}
}
/**
* Reads the path from an object up to a given path index.
*
* @throws UnexpectedTypeException if a value within the path is neither object nor array
* @throws NoSuchIndexException If a non-existing index is accessed
*/
private function readPropertiesUntil(array $zval, PropertyPathInterface $propertyPath, int $lastIndex, bool $ignoreInvalidIndices = true): array
{
if (!\is_object($zval[self::VALUE]) && !\is_array($zval[self::VALUE])) {
throw new UnexpectedTypeException($zval[self::VALUE], $propertyPath, 0);
}
// Add the root object to the list
$propertyValues = [$zval];
for ($i = 0; $i < $lastIndex; ++$i) {
$property = $propertyPath->getElement($i);
$isIndex = $propertyPath->isIndex($i);
if ($isIndex) {
// Create missing nested arrays on demand
if (($zval[self::VALUE] instanceof \ArrayAccess && !$zval[self::VALUE]->offsetExists($property)) ||
(\is_array($zval[self::VALUE]) && !isset($zval[self::VALUE][$property]) && !\array_key_exists($property, $zval[self::VALUE]))
) {
if (!$ignoreInvalidIndices) {
if (!\is_array($zval[self::VALUE])) {
if (!$zval[self::VALUE] instanceof \Traversable) {
throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s".', $property, (string) $propertyPath));
}
$zval[self::VALUE] = iterator_to_array($zval[self::VALUE]);
}
throw new NoSuchIndexException(sprintf('Cannot read index "%s" while trying to traverse path "%s". Available indices are "%s".', $property, (string) $propertyPath, print_r(array_keys($zval[self::VALUE]), true)));
}
if ($i + 1 < $propertyPath->getLength()) {
if (isset($zval[self::REF])) {
$zval[self::VALUE][$property] = [];
$zval[self::REF] = $zval[self::VALUE];
} else {
$zval[self::VALUE] = [$property => []];
}
}
}
$zval = $this->readIndex($zval, $property);
} else {
$zval = $this->readProperty($zval, $property, $this->ignoreInvalidProperty);
}
// the final value of the path must not be validated
if ($i + 1 < $propertyPath->getLength() && !\is_object($zval[self::VALUE]) && !\is_array($zval[self::VALUE])) {
throw new UnexpectedTypeException($zval[self::VALUE], $propertyPath, $i + 1);
}
if (isset($zval[self::REF]) && (0 === $i || isset($propertyValues[$i - 1][self::IS_REF_CHAINED]))) {
// Set the IS_REF_CHAINED flag to true if:
// current property is passed by reference and
// it is the first element in the property path or
// the IS_REF_CHAINED flag of its parent element is true
// Basically, this flag is true only when the reference chain from the top element to current element is not broken
$zval[self::IS_REF_CHAINED] = true;
}
$propertyValues[] = $zval;
}
return $propertyValues;
}
/**
* Reads a key from an array-like structure.
*
* @param string|int $index The key to read
*
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
*/
private function readIndex(array $zval, $index): array
{
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
throw new NoSuchIndexException(sprintf('Cannot read index "%s" from object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_debug_type($zval[self::VALUE])));
}
$result = self::RESULT_PROTO;
if (isset($zval[self::VALUE][$index])) {
$result[self::VALUE] = $zval[self::VALUE][$index];
if (!isset($zval[self::REF])) {
// Save creating references when doing read-only lookups
} elseif (\is_array($zval[self::VALUE])) {
$result[self::REF] = &$zval[self::REF][$index];
} elseif (\is_object($result[self::VALUE])) {
$result[self::REF] = $result[self::VALUE];
}
}
return $result;
}
/**
* Reads the value of a property from an object.
*
* @throws NoSuchPropertyException If $ignoreInvalidProperty is false and the property does not exist or is not public
*/
private function readProperty(array $zval, string $property, bool $ignoreInvalidProperty = false): array
{
if (!\is_object($zval[self::VALUE])) {
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%1$s]" instead.', $property));
}
$result = self::RESULT_PROTO;
$object = $zval[self::VALUE];
$class = \get_class($object);
$access = $this->getReadInfo($class, $property);
if (null !== $access) {
$name = $access->getName();
$type = $access->getType();
try {
if (PropertyReadInfo::TYPE_METHOD === $type) {
try {
$result[self::VALUE] = $object->$name();
} catch (\TypeError $e) {
[$trace] = $e->getTrace();
// handle uninitialized properties in PHP >= 7
if (__FILE__ === $trace['file']
&& $name === $trace['function']
&& $object instanceof $trace['class']
&& preg_match('/Return value (?:of .*::\w+\(\) )?must be of (?:the )?type (\w+), null returned$/', $e->getMessage(), $matches)
) {
throw new UninitializedPropertyException(sprintf('The method "%s::%s()" returned "null", but expected type "%3$s". Did you forget to initialize a property or to make the return type nullable using "?%3$s"?', get_debug_type($object), $name, $matches[1]), 0, $e);
}
throw $e;
}
} elseif (PropertyReadInfo::TYPE_PROPERTY === $type) {
if ($access->canBeReference() && !isset($object->$name) && !\array_key_exists($name, (array) $object) && (\PHP_VERSION_ID < 70400 || !(new \ReflectionProperty($class, $name))->hasType())) {
throw new UninitializedPropertyException(sprintf('The property "%s::$%s" is not initialized.', $class, $name));
}
$result[self::VALUE] = $object->$name;
if (isset($zval[self::REF]) && $access->canBeReference()) {
$result[self::REF] = &$object->$name;
}
}
} catch (\Error $e) {
// handle uninitialized properties in PHP >= 7.4
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
$r = new \ReflectionProperty(str_contains($matches[1], '@anonymous') ? $class : $matches[1], $matches[2]);
$type = ($type = $r->getType()) instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
throw new UninitializedPropertyException(sprintf('The property "%s::$%s" is not readable because it is typed "%s". You should initialize it or declare a default value instead.', $matches[1], $r->getName(), $type), 0, $e);
}
throw $e;
}
} elseif (property_exists($object, $property) && \array_key_exists($property, (array) $object)) {
$result[self::VALUE] = $object->$property;
if (isset($zval[self::REF])) {
$result[self::REF] = &$object->$property;
}
} elseif (!$ignoreInvalidProperty) {
throw new NoSuchPropertyException(sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class));
}
// Objects are always passed around by reference
if (isset($zval[self::REF]) && \is_object($result[self::VALUE])) {
$result[self::REF] = $result[self::VALUE];
}
return $result;
}
/**
* Guesses how to read the property value.
*/
private function getReadInfo(string $class, string $property): ?PropertyReadInfo
{
$key = str_replace('\\', '.', $class).'..'.$property;
if (isset($this->readPropertyCache[$key])) {
return $this->readPropertyCache[$key];
}
if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem(self::CACHE_PREFIX_READ.rawurlencode($key));
if ($item->isHit()) {
return $this->readPropertyCache[$key] = $item->get();
}
}
$accessor = $this->readInfoExtractor->getReadInfo($class, $property, [
'enable_getter_setter_extraction' => true,
'enable_magic_methods_extraction' => $this->magicMethodsFlags,
'enable_constructor_extraction' => false,
]);
if (isset($item)) {
$this->cacheItemPool->save($item->set($accessor));
}
return $this->readPropertyCache[$key] = $accessor;
}
/**
* Sets the value of an index in a given array-accessible value.
*
* @param string|int $index The index to write at
* @param mixed $value The value to write
*
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
*/
private function writeIndex(array $zval, $index, $value)
{
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
throw new NoSuchIndexException(sprintf('Cannot modify index "%s" in object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_debug_type($zval[self::VALUE])));
}
$zval[self::REF][$index] = $value;
}
/**
* Sets the value of a property in the given object.
*
* @param mixed $value The value to write
*
* @throws NoSuchPropertyException if the property does not exist or is not public
*/
private function writeProperty(array $zval, string $property, $value)
{
if (!\is_object($zval[self::VALUE])) {
throw new NoSuchPropertyException(sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%1$s]" instead?', $property));
}
$object = $zval[self::VALUE];
$class = \get_class($object);
$mutator = $this->getWriteInfo($class, $property, $value);
if (PropertyWriteInfo::TYPE_NONE !== $mutator->getType()) {
$type = $mutator->getType();
if (PropertyWriteInfo::TYPE_METHOD === $type) {
$object->{$mutator->getName()}($value);
} elseif (PropertyWriteInfo::TYPE_PROPERTY === $type) {
$object->{$mutator->getName()} = $value;
} elseif (PropertyWriteInfo::TYPE_ADDER_AND_REMOVER === $type) {
$this->writeCollection($zval, $property, $value, $mutator->getAdderInfo(), $mutator->getRemoverInfo());
}
} elseif ($object instanceof \stdClass && property_exists($object, $property)) {
$object->$property = $value;
} elseif (!$this->ignoreInvalidProperty) {
if ($mutator->hasErrors()) {
throw new NoSuchPropertyException(implode('. ', $mutator->getErrors()).'.');
}
throw new NoSuchPropertyException(sprintf('Could not determine access type for property "%s" in class "%s".', $property, get_debug_type($object)));
}
}
/**
* Adjusts a collection-valued property by calling add*() and remove*() methods.
*/
private function writeCollection(array $zval, string $property, iterable $collection, PropertyWriteInfo $addMethod, PropertyWriteInfo $removeMethod)
{
// At this point the add and remove methods have been found
$previousValue = $this->readProperty($zval, $property);
$previousValue = $previousValue[self::VALUE];
$removeMethodName = $removeMethod->getName();
$addMethodName = $addMethod->getName();
if ($previousValue instanceof \Traversable) {
$previousValue = iterator_to_array($previousValue);
}
if ($previousValue && \is_array($previousValue)) {
if (\is_object($collection)) {
$collection = iterator_to_array($collection);
}
foreach ($previousValue as $key => $item) {
if (!\in_array($item, $collection, true)) {
unset($previousValue[$key]);
$zval[self::VALUE]->$removeMethodName($item);
}
}
} else {
$previousValue = false;
}
foreach ($collection as $item) {
if (!$previousValue || !\in_array($item, $previousValue, true)) {
$zval[self::VALUE]->$addMethodName($item);
}
}
}
private function getWriteInfo(string $class, string $property, $value): PropertyWriteInfo
{
$useAdderAndRemover = is_iterable($value);
$key = str_replace('\\', '.', $class).'..'.$property.'..'.(int) $useAdderAndRemover;
if (isset($this->writePropertyCache[$key])) {
return $this->writePropertyCache[$key];
}
if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem(self::CACHE_PREFIX_WRITE.rawurlencode($key));
if ($item->isHit()) {
return $this->writePropertyCache[$key] = $item->get();
}
}
$mutator = $this->writeInfoExtractor->getWriteInfo($class, $property, [
'enable_getter_setter_extraction' => true,
'enable_magic_methods_extraction' => $this->magicMethodsFlags,
'enable_constructor_extraction' => false,
'enable_adder_remover_extraction' => $useAdderAndRemover,
]);
if (isset($item)) {
$this->cacheItemPool->save($item->set($mutator));
}
return $this->writePropertyCache[$key] = $mutator;
}
/**
* Returns whether a property is writable in the given object.
*/
private function isPropertyWritable(object $object, string $property): bool
{
$mutatorForArray = $this->getWriteInfo(\get_class($object), $property, []);
if (PropertyWriteInfo::TYPE_NONE !== $mutatorForArray->getType() || ($object instanceof \stdClass && property_exists($object, $property))) {
return true;
}
$mutator = $this->getWriteInfo(\get_class($object), $property, '');
return PropertyWriteInfo::TYPE_NONE !== $mutator->getType() || ($object instanceof \stdClass && property_exists($object, $property));
}
/**
* Gets a PropertyPath instance and caches it.
*
* @param string|PropertyPath $propertyPath
*/
private function getPropertyPath($propertyPath): PropertyPath
{
if ($propertyPath instanceof PropertyPathInterface) {
// Don't call the copy constructor has it is not needed here
return $propertyPath;
}
if (isset($this->propertyPathCache[$propertyPath])) {
return $this->propertyPathCache[$propertyPath];
}
if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem(self::CACHE_PREFIX_PROPERTY_PATH.rawurlencode($propertyPath));
if ($item->isHit()) {
return $this->propertyPathCache[$propertyPath] = $item->get();
}
}
$propertyPathInstance = new PropertyPath($propertyPath);
if (isset($item)) {
$item->set($propertyPathInstance);
$this->cacheItemPool->save($item);
}
return $this->propertyPathCache[$propertyPath] = $propertyPathInstance;
}
/**
* Creates the APCu adapter if applicable.
*
* @return AdapterInterface
*
* @throws \LogicException When the Cache Component isn't available
*/
public static function createCache(string $namespace, int $defaultLifetime, string $version, LoggerInterface $logger = null)
{
if (!class_exists(ApcuAdapter::class)) {
throw new \LogicException(sprintf('The Symfony Cache component must be installed to use "%s()".', __METHOD__));
}
if (!ApcuAdapter::isSupported()) {
return new NullAdapter();
}
$apcu = new ApcuAdapter($namespace, $defaultLifetime / 5, $version);
if ('cli' === \PHP_SAPI && !filter_var(ini_get('apc.enable_cli'), \FILTER_VALIDATE_BOOLEAN)) {
$apcu->setLogger(new NullLogger());
} elseif (null !== $logger) {
$apcu->setLogger($logger);
}
return $apcu;
}
}

View File

@ -0,0 +1,308 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\PropertyInfo\PropertyReadInfoExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyWriteInfoExtractorInterface;
/**
* A configurable builder to create a PropertyAccessor.
*
* @author Jérémie Augustin <jeremie.augustin@pixel-cookers.com>
*/
class PropertyAccessorBuilder
{
/** @var int */
private $magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET;
private $throwExceptionOnInvalidIndex = false;
private $throwExceptionOnInvalidPropertyPath = true;
/**
* @var CacheItemPoolInterface|null
*/
private $cacheItemPool;
/**
* @var PropertyReadInfoExtractorInterface|null
*/
private $readInfoExtractor;
/**
* @var PropertyWriteInfoExtractorInterface|null
*/
private $writeInfoExtractor;
/**
* Enables the use of all magic methods by the PropertyAccessor.
*
* @return $this
*/
public function enableMagicMethods(): self
{
$this->magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET | PropertyAccessor::MAGIC_CALL;
return $this;
}
/**
* Disable the use of all magic methods by the PropertyAccessor.
*
* @return $this
*/
public function disableMagicMethods(): self
{
$this->magicMethods = PropertyAccessor::DISALLOW_MAGIC_METHODS;
return $this;
}
/**
* Enables the use of "__call" by the PropertyAccessor.
*
* @return $this
*/
public function enableMagicCall()
{
$this->magicMethods |= PropertyAccessor::MAGIC_CALL;
return $this;
}
/**
* Enables the use of "__get" by the PropertyAccessor.
*/
public function enableMagicGet(): self
{
$this->magicMethods |= PropertyAccessor::MAGIC_GET;
return $this;
}
/**
* Enables the use of "__set" by the PropertyAccessor.
*
* @return $this
*/
public function enableMagicSet(): self
{
$this->magicMethods |= PropertyAccessor::MAGIC_SET;
return $this;
}
/**
* Disables the use of "__call" by the PropertyAccessor.
*
* @return $this
*/
public function disableMagicCall()
{
$this->magicMethods &= ~PropertyAccessor::MAGIC_CALL;
return $this;
}
/**
* Disables the use of "__get" by the PropertyAccessor.
*
* @return $this
*/
public function disableMagicGet(): self
{
$this->magicMethods &= ~PropertyAccessor::MAGIC_GET;
return $this;
}
/**
* Disables the use of "__set" by the PropertyAccessor.
*
* @return $this
*/
public function disableMagicSet(): self
{
$this->magicMethods &= ~PropertyAccessor::MAGIC_SET;
return $this;
}
/**
* @return bool whether the use of "__call" by the PropertyAccessor is enabled
*/
public function isMagicCallEnabled()
{
return (bool) ($this->magicMethods & PropertyAccessor::MAGIC_CALL);
}
/**
* @return bool whether the use of "__get" by the PropertyAccessor is enabled
*/
public function isMagicGetEnabled(): bool
{
return $this->magicMethods & PropertyAccessor::MAGIC_GET;
}
/**
* @return bool whether the use of "__set" by the PropertyAccessor is enabled
*/
public function isMagicSetEnabled(): bool
{
return $this->magicMethods & PropertyAccessor::MAGIC_SET;
}
/**
* Enables exceptions when reading a non-existing index.
*
* This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
* which are always created on-the-fly.
*
* @return $this
*/
public function enableExceptionOnInvalidIndex()
{
$this->throwExceptionOnInvalidIndex = true;
return $this;
}
/**
* Disables exceptions when reading a non-existing index.
*
* Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
*
* @return $this
*/
public function disableExceptionOnInvalidIndex()
{
$this->throwExceptionOnInvalidIndex = false;
return $this;
}
/**
* @return bool whether an exception is thrown or null is returned when reading a non-existing index
*/
public function isExceptionOnInvalidIndexEnabled()
{
return $this->throwExceptionOnInvalidIndex;
}
/**
* Enables exceptions when reading a non-existing property.
*
* This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
* which are always created on-the-fly.
*
* @return $this
*/
public function enableExceptionOnInvalidPropertyPath()
{
$this->throwExceptionOnInvalidPropertyPath = true;
return $this;
}
/**
* Disables exceptions when reading a non-existing index.
*
* Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
*
* @return $this
*/
public function disableExceptionOnInvalidPropertyPath()
{
$this->throwExceptionOnInvalidPropertyPath = false;
return $this;
}
/**
* @return bool whether an exception is thrown or null is returned when reading a non-existing property
*/
public function isExceptionOnInvalidPropertyPath()
{
return $this->throwExceptionOnInvalidPropertyPath;
}
/**
* Sets a cache system.
*
* @return $this
*/
public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool = null)
{
$this->cacheItemPool = $cacheItemPool;
return $this;
}
/**
* Gets the used cache system.
*
* @return CacheItemPoolInterface|null
*/
public function getCacheItemPool()
{
return $this->cacheItemPool;
}
/**
* @return $this
*/
public function setReadInfoExtractor(?PropertyReadInfoExtractorInterface $readInfoExtractor)
{
$this->readInfoExtractor = $readInfoExtractor;
return $this;
}
public function getReadInfoExtractor(): ?PropertyReadInfoExtractorInterface
{
return $this->readInfoExtractor;
}
/**
* @return $this
*/
public function setWriteInfoExtractor(?PropertyWriteInfoExtractorInterface $writeInfoExtractor)
{
$this->writeInfoExtractor = $writeInfoExtractor;
return $this;
}
public function getWriteInfoExtractor(): ?PropertyWriteInfoExtractorInterface
{
return $this->writeInfoExtractor;
}
/**
* Builds and returns a new PropertyAccessor object.
*
* @return PropertyAccessorInterface
*/
public function getPropertyAccessor()
{
$throw = PropertyAccessor::DO_NOT_THROW;
if ($this->throwExceptionOnInvalidIndex) {
$throw |= PropertyAccessor::THROW_ON_INVALID_INDEX;
}
if ($this->throwExceptionOnInvalidPropertyPath) {
$throw |= PropertyAccessor::THROW_ON_INVALID_PROPERTY_PATH;
}
return new PropertyAccessor($this->magicMethods, $throw, $this->cacheItemPool, $this->readInfoExtractor, $this->writeInfoExtractor);
}
}

View File

@ -0,0 +1,114 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
/**
* Writes and reads values to/from an object/array graph.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface PropertyAccessorInterface
{
/**
* Sets the value at the end of the property path of the object graph.
*
* Example:
*
* use Symfony\Component\PropertyAccess\PropertyAccess;
*
* $propertyAccessor = PropertyAccess::createPropertyAccessor();
*
* echo $propertyAccessor->setValue($object, 'child.name', 'Fabien');
* // equals echo $object->getChild()->setName('Fabien');
*
* This method first tries to find a public setter for each property in the
* path. The name of the setter must be the camel-cased property name
* prefixed with "set".
*
* If the setter does not exist, this method tries to find a public
* property. The value of the property is then changed.
*
* If neither is found, an exception is thrown.
*
* @param object|array $objectOrArray The object or array to modify
* @param string|PropertyPathInterface $propertyPath The property path to modify
* @param mixed $value The value to set at the end of the property path
*
* @throws Exception\InvalidArgumentException If the property path is invalid
* @throws Exception\AccessException If a property/index does not exist or is not public
* @throws Exception\UnexpectedTypeException If a value within the path is neither object nor array
*/
public function setValue(&$objectOrArray, $propertyPath, $value);
/**
* Returns the value at the end of the property path of the object graph.
*
* Example:
*
* use Symfony\Component\PropertyAccess\PropertyAccess;
*
* $propertyAccessor = PropertyAccess::createPropertyAccessor();
*
* echo $propertyAccessor->getValue($object, 'child.name');
* // equals echo $object->getChild()->getName();
*
* This method first tries to find a public getter for each property in the
* path. The name of the getter must be the camel-cased property name
* prefixed with "get", "is", or "has".
*
* If the getter does not exist, this method tries to find a public
* property. The value of the property is then returned.
*
* If none of them are found, an exception is thrown.
*
* @param object|array $objectOrArray The object or array to traverse
* @param string|PropertyPathInterface $propertyPath The property path to read
*
* @return mixed
*
* @throws Exception\InvalidArgumentException If the property path is invalid
* @throws Exception\AccessException If a property/index does not exist or is not public
* @throws Exception\UnexpectedTypeException If a value within the path is neither object
* nor array
*/
public function getValue($objectOrArray, $propertyPath);
/**
* Returns whether a value can be written at a given property path.
*
* Whenever this method returns true, {@link setValue()} is guaranteed not
* to throw an exception when called with the same arguments.
*
* @param object|array $objectOrArray The object or array to check
* @param string|PropertyPathInterface $propertyPath The property path to check
*
* @return bool
*
* @throws Exception\InvalidArgumentException If the property path is invalid
*/
public function isWritable($objectOrArray, $propertyPath);
/**
* Returns whether a property path can be read from an object graph.
*
* Whenever this method returns true, {@link getValue()} is guaranteed not
* to throw an exception when called with the same arguments.
*
* @param object|array $objectOrArray The object or array to check
* @param string|PropertyPathInterface $propertyPath The property path to check
*
* @return bool
*
* @throws Exception\InvalidArgumentException If the property path is invalid
*/
public function isReadable($objectOrArray, $propertyPath);
}

View File

@ -0,0 +1,208 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
use Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException;
use Symfony\Component\PropertyAccess\Exception\OutOfBoundsException;
/**
* Default implementation of {@link PropertyPathInterface}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @implements \IteratorAggregate<int, string>
*/
class PropertyPath implements \IteratorAggregate, PropertyPathInterface
{
/**
* Character used for separating between plural and singular of an element.
*/
public const SINGULAR_SEPARATOR = '|';
/**
* The elements of the property path.
*
* @var list<string>
*/
private $elements = [];
/**
* The number of elements in the property path.
*
* @var int
*/
private $length;
/**
* Contains a Boolean for each property in $elements denoting whether this
* element is an index. It is a property otherwise.
*
* @var array
*/
private $isIndex = [];
/**
* String representation of the path.
*
* @var string
*/
private $pathAsString;
/**
* Constructs a property path from a string.
*
* @param PropertyPath|string $propertyPath The property path as string or instance
*
* @throws InvalidArgumentException If the given path is not a string
* @throws InvalidPropertyPathException If the syntax of the property path is not valid
*/
public function __construct($propertyPath)
{
// Can be used as copy constructor
if ($propertyPath instanceof self) {
/* @var PropertyPath $propertyPath */
$this->elements = $propertyPath->elements;
$this->length = $propertyPath->length;
$this->isIndex = $propertyPath->isIndex;
$this->pathAsString = $propertyPath->pathAsString;
return;
}
if (!\is_string($propertyPath)) {
throw new InvalidArgumentException(sprintf('The property path constructor needs a string or an instance of "Symfony\Component\PropertyAccess\PropertyPath". Got: "%s".', get_debug_type($propertyPath)));
}
if ('' === $propertyPath) {
throw new InvalidPropertyPathException('The property path should not be empty.');
}
$this->pathAsString = $propertyPath;
$position = 0;
$remaining = $propertyPath;
// first element is evaluated differently - no leading dot for properties
$pattern = '/^(([^\.\[]++)|\[([^\]]++)\])(.*)/';
while (preg_match($pattern, $remaining, $matches)) {
if ('' !== $matches[2]) {
$element = $matches[2];
$this->isIndex[] = false;
} else {
$element = $matches[3];
$this->isIndex[] = true;
}
$this->elements[] = $element;
$position += \strlen($matches[1]);
$remaining = $matches[4];
$pattern = '/^(\.([^\.|\[]++)|\[([^\]]++)\])(.*)/';
}
if ('' !== $remaining) {
throw new InvalidPropertyPathException(sprintf('Could not parse property path "%s". Unexpected token "%s" at position %d.', $propertyPath, $remaining[0], $position));
}
$this->length = \count($this->elements);
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->pathAsString;
}
/**
* {@inheritdoc}
*/
public function getLength()
{
return $this->length;
}
/**
* {@inheritdoc}
*/
public function getParent()
{
if ($this->length <= 1) {
return null;
}
$parent = clone $this;
--$parent->length;
$parent->pathAsString = substr($parent->pathAsString, 0, max(strrpos($parent->pathAsString, '.'), strrpos($parent->pathAsString, '[')));
array_pop($parent->elements);
array_pop($parent->isIndex);
return $parent;
}
/**
* Returns a new iterator for this path.
*
* @return PropertyPathIteratorInterface
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new PropertyPathIterator($this);
}
/**
* {@inheritdoc}
*/
public function getElements()
{
return $this->elements;
}
/**
* {@inheritdoc}
*/
public function getElement(int $index)
{
if (!isset($this->elements[$index])) {
throw new OutOfBoundsException(sprintf('The index "%s" is not within the property path.', $index));
}
return $this->elements[$index];
}
/**
* {@inheritdoc}
*/
public function isProperty(int $index)
{
if (!isset($this->isIndex[$index])) {
throw new OutOfBoundsException(sprintf('The index "%s" is not within the property path.', $index));
}
return !$this->isIndex[$index];
}
/**
* {@inheritdoc}
*/
public function isIndex(int $index)
{
if (!isset($this->isIndex[$index])) {
throw new OutOfBoundsException(sprintf('The index "%s" is not within the property path.', $index));
}
return $this->isIndex[$index];
}
}

View File

@ -0,0 +1,281 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
use Symfony\Component\PropertyAccess\Exception\OutOfBoundsException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PropertyPathBuilder
{
private $elements = [];
private $isIndex = [];
/**
* Creates a new property path builder.
*
* @param PropertyPathInterface|string|null $path The path to initially store
* in the builder. Optional.
*/
public function __construct($path = null)
{
if (null !== $path) {
$this->append($path);
}
}
/**
* Appends a (sub-) path to the current path.
*
* @param PropertyPathInterface|string $path The path to append
* @param int $offset The offset where the appended
* piece starts in $path
* @param int $length The length of the appended piece
* If 0, the full path is appended
*/
public function append($path, int $offset = 0, int $length = 0)
{
if (\is_string($path)) {
$path = new PropertyPath($path);
}
if (0 === $length) {
$end = $path->getLength();
} else {
$end = $offset + $length;
}
for (; $offset < $end; ++$offset) {
$this->elements[] = $path->getElement($offset);
$this->isIndex[] = $path->isIndex($offset);
}
}
/**
* Appends an index element to the current path.
*/
public function appendIndex(string $name)
{
$this->elements[] = $name;
$this->isIndex[] = true;
}
/**
* Appends a property element to the current path.
*/
public function appendProperty(string $name)
{
$this->elements[] = $name;
$this->isIndex[] = false;
}
/**
* Removes elements from the current path.
*
* @throws OutOfBoundsException if offset is invalid
*/
public function remove(int $offset, int $length = 1)
{
if (!isset($this->elements[$offset])) {
throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
}
$this->resize($offset, $length, 0);
}
/**
* Replaces a sub-path by a different (sub-) path.
*
* @param int $offset The offset at which to replace
* @param int $length The length of the piece to replace
* @param PropertyPathInterface|string $path The path to insert
* @param int $pathOffset The offset where the inserted piece
* starts in $path
* @param int $pathLength The length of the inserted piece
* If 0, the full path is inserted
*
* @throws OutOfBoundsException If the offset is invalid
*/
public function replace(int $offset, int $length, $path, int $pathOffset = 0, int $pathLength = 0)
{
if (\is_string($path)) {
$path = new PropertyPath($path);
}
if ($offset < 0 && abs($offset) <= $this->getLength()) {
$offset = $this->getLength() + $offset;
} elseif (!isset($this->elements[$offset])) {
throw new OutOfBoundsException('The offset '.$offset.' is not within the property path');
}
if (0 === $pathLength) {
$pathLength = $path->getLength() - $pathOffset;
}
$this->resize($offset, $length, $pathLength);
for ($i = 0; $i < $pathLength; ++$i) {
$this->elements[$offset + $i] = $path->getElement($pathOffset + $i);
$this->isIndex[$offset + $i] = $path->isIndex($pathOffset + $i);
}
ksort($this->elements);
}
/**
* Replaces a property element by an index element.
*
* @throws OutOfBoundsException If the offset is invalid
*/
public function replaceByIndex(int $offset, string $name = null)
{
if (!isset($this->elements[$offset])) {
throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
}
if (null !== $name) {
$this->elements[$offset] = $name;
}
$this->isIndex[$offset] = true;
}
/**
* Replaces an index element by a property element.
*
* @throws OutOfBoundsException If the offset is invalid
*/
public function replaceByProperty(int $offset, string $name = null)
{
if (!isset($this->elements[$offset])) {
throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
}
if (null !== $name) {
$this->elements[$offset] = $name;
}
$this->isIndex[$offset] = false;
}
/**
* Returns the length of the current path.
*
* @return int
*/
public function getLength()
{
return \count($this->elements);
}
/**
* Returns the current property path.
*
* @return PropertyPathInterface|null
*/
public function getPropertyPath()
{
$pathAsString = $this->__toString();
return '' !== $pathAsString ? new PropertyPath($pathAsString) : null;
}
/**
* Returns the current property path as string.
*
* @return string
*/
public function __toString()
{
$string = '';
foreach ($this->elements as $offset => $element) {
if ($this->isIndex[$offset]) {
$element = '['.$element.']';
} elseif ('' !== $string) {
$string .= '.';
}
$string .= $element;
}
return $string;
}
/**
* Resizes the path so that a chunk of length $cutLength is
* removed at $offset and another chunk of length $insertionLength
* can be inserted.
*/
private function resize(int $offset, int $cutLength, int $insertionLength)
{
// Nothing else to do in this case
if ($insertionLength === $cutLength) {
return;
}
$length = \count($this->elements);
if ($cutLength > $insertionLength) {
// More elements should be removed than inserted
$diff = $cutLength - $insertionLength;
$newLength = $length - $diff;
// Shift elements to the left (left-to-right until the new end)
// Max allowed offset to be shifted is such that
// $offset + $diff < $length (otherwise invalid index access)
// i.e. $offset < $length - $diff = $newLength
for ($i = $offset; $i < $newLength; ++$i) {
$this->elements[$i] = $this->elements[$i + $diff];
$this->isIndex[$i] = $this->isIndex[$i + $diff];
}
// All remaining elements should be removed
$this->elements = \array_slice($this->elements, 0, $i);
$this->isIndex = \array_slice($this->isIndex, 0, $i);
} else {
$diff = $insertionLength - $cutLength;
$newLength = $length + $diff;
$indexAfterInsertion = $offset + $insertionLength;
// $diff <= $insertionLength
// $indexAfterInsertion >= $insertionLength
// => $diff <= $indexAfterInsertion
// In each of the following loops, $i >= $diff must hold,
// otherwise ($i - $diff) becomes negative.
// Shift old elements to the right to make up space for the
// inserted elements. This needs to be done left-to-right in
// order to preserve an ascending array index order
// Since $i = max($length, $indexAfterInsertion) and $indexAfterInsertion >= $diff,
// $i >= $diff is guaranteed.
for ($i = max($length, $indexAfterInsertion); $i < $newLength; ++$i) {
$this->elements[$i] = $this->elements[$i - $diff];
$this->isIndex[$i] = $this->isIndex[$i - $diff];
}
// Shift remaining elements to the right. Do this right-to-left
// so we don't overwrite elements before copying them
// The last written index is the immediate index after the inserted
// string, because the indices before that will be overwritten
// anyway.
// Since $i >= $indexAfterInsertion and $indexAfterInsertion >= $diff,
// $i >= $diff is guaranteed.
for ($i = $length - 1; $i >= $indexAfterInsertion; --$i) {
$this->elements[$i] = $this->elements[$i - $diff];
$this->isIndex[$i] = $this->isIndex[$i - $diff];
}
}
}
}

View File

@ -0,0 +1,88 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
/**
* A sequence of property names or array indices.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @extends \Traversable<int, string>
*/
interface PropertyPathInterface extends \Traversable
{
/**
* Returns the string representation of the property path.
*
* @return string
*/
public function __toString();
/**
* Returns the length of the property path, i.e. the number of elements.
*
* @return int
*/
public function getLength();
/**
* Returns the parent property path.
*
* The parent property path is the one that contains the same items as
* this one except for the last one.
*
* If this property path only contains one item, null is returned.
*
* @return self|null
*/
public function getParent();
/**
* Returns the elements of the property path as array.
*
* @return list<string>
*/
public function getElements();
/**
* Returns the element at the given index in the property path.
*
* @param int $index The index key
*
* @return string
*
* @throws Exception\OutOfBoundsException If the offset is invalid
*/
public function getElement(int $index);
/**
* Returns whether the element at the given index is a property.
*
* @param int $index The index in the property path
*
* @return bool
*
* @throws Exception\OutOfBoundsException If the offset is invalid
*/
public function isProperty(int $index);
/**
* Returns whether the element at the given index is an array index.
*
* @param int $index The index in the property path
*
* @return bool
*
* @throws Exception\OutOfBoundsException If the offset is invalid
*/
public function isIndex(int $index);
}

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
/**
* Traverses a property path and provides additional methods to find out
* information about the current element.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @extends \ArrayIterator<int, string>
*/
class PropertyPathIterator extends \ArrayIterator implements PropertyPathIteratorInterface
{
protected $path;
public function __construct(PropertyPathInterface $path)
{
parent::__construct($path->getElements());
$this->path = $path;
}
/**
* {@inheritdoc}
*/
public function isIndex()
{
return $this->path->isIndex($this->key());
}
/**
* {@inheritdoc}
*/
public function isProperty()
{
return $this->path->isProperty($this->key());
}
}

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @extends \SeekableIterator<int, string>
*/
interface PropertyPathIteratorInterface extends \SeekableIterator
{
/**
* Returns whether the current element in the property path is an array
* index.
*
* @return bool
*/
public function isIndex();
/**
* Returns whether the current element in the property path is a property
* name.
*
* @return bool
*/
public function isProperty();
}

View File

@ -0,0 +1,14 @@
PropertyAccess Component
========================
The PropertyAccess component provides functions to read and write from/to an
object or array using a simple string notation.
Resources
---------
* [Documentation](https://symfony.com/doc/current/components/property_access.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)

View File

@ -0,0 +1,37 @@
{
"name": "symfony/property-access",
"type": "library",
"description": "Provides functions to read and write from/to an object or array using a simple string notation",
"keywords": ["property", "index", "access", "object", "array", "extraction", "injection", "reflection", "property path"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.1|^3",
"symfony/polyfill-php80": "^1.16",
"symfony/property-info": "^5.2|^6.0"
},
"require-dev": {
"symfony/cache": "^4.4|^5.0|^6.0"
},
"suggest": {
"psr/cache-implementation": "To cache access methods."
},
"autoload": {
"psr-4": { "Symfony\\Component\\PropertyAccess\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}