| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace WindPressDeps\Symfony\Component\PropertyAccess; |
| 12 |
|
| 13 |
use WindPressDeps\Symfony\Component\PropertyAccess\Exception\InvalidArgumentException; |
| 14 |
use WindPressDeps\Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException; |
| 15 |
use WindPressDeps\Symfony\Component\PropertyAccess\Exception\OutOfBoundsException; |
| 16 |
/** |
| 17 |
* Default implementation of {@link PropertyPathInterface}. |
| 18 |
* |
| 19 |
* @author Bernhard Schussek <bschussek@gmail.com> |
| 20 |
* |
| 21 |
* @implements \IteratorAggregate<int, string> |
| 22 |
*/ |
| 23 |
class PropertyPath implements \IteratorAggregate, PropertyPathInterface |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Character used for separating between plural and singular of an element. |
| 27 |
*/ |
| 28 |
public const SINGULAR_SEPARATOR = '|'; |
| 29 |
/** |
| 30 |
* The elements of the property path. |
| 31 |
* |
| 32 |
* @var list<string> |
| 33 |
*/ |
| 34 |
private $elements = []; |
| 35 |
/** |
| 36 |
* The number of elements in the property path. |
| 37 |
* |
| 38 |
* @var int |
| 39 |
*/ |
| 40 |
private $length; |
| 41 |
/** |
| 42 |
* Contains a Boolean for each property in $elements denoting whether this |
| 43 |
* element is an index. It is a property otherwise. |
| 44 |
* |
| 45 |
* @var array |
| 46 |
*/ |
| 47 |
private $isIndex = []; |
| 48 |
/** |
| 49 |
* String representation of the path. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
private $pathAsString; |
| 54 |
/** |
| 55 |
* Constructs a property path from a string. |
| 56 |
* |
| 57 |
* @param PropertyPath|string $propertyPath The property path as string or instance |
| 58 |
* |
| 59 |
* @throws InvalidArgumentException If the given path is not a string |
| 60 |
* @throws InvalidPropertyPathException If the syntax of the property path is not valid |
| 61 |
*/ |
| 62 |
public function __construct($propertyPath) |
| 63 |
{ |
| 64 |
// Can be used as copy constructor |
| 65 |
if ($propertyPath instanceof self) { |
| 66 |
/* @var PropertyPath $propertyPath */ |
| 67 |
$this->elements = $propertyPath->elements; |
| 68 |
$this->length = $propertyPath->length; |
| 69 |
$this->isIndex = $propertyPath->isIndex; |
| 70 |
$this->pathAsString = $propertyPath->pathAsString; |
| 71 |
return; |
| 72 |
} |
| 73 |
if (!\is_string($propertyPath)) { |
| 74 |
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))); |
| 75 |
} |
| 76 |
if ('' === $propertyPath) { |
| 77 |
throw new InvalidPropertyPathException('The property path should not be empty.'); |
| 78 |
} |
| 79 |
$this->pathAsString = $propertyPath; |
| 80 |
$position = 0; |
| 81 |
$remaining = $propertyPath; |
| 82 |
// first element is evaluated differently - no leading dot for properties |
| 83 |
$pattern = '/^(([^\\.\\[]++)|\\[([^\\]]++)\\])(.*)/'; |
| 84 |
while (\preg_match($pattern, $remaining, $matches)) { |
| 85 |
if ('' !== $matches[2]) { |
| 86 |
$element = $matches[2]; |
| 87 |
$this->isIndex[] = \false; |
| 88 |
} else { |
| 89 |
$element = $matches[3]; |
| 90 |
$this->isIndex[] = \true; |
| 91 |
} |
| 92 |
$this->elements[] = $element; |
| 93 |
$position += \strlen($matches[1]); |
| 94 |
$remaining = $matches[4]; |
| 95 |
$pattern = '/^(\\.([^\\.|\\[]++)|\\[([^\\]]++)\\])(.*)/'; |
| 96 |
} |
| 97 |
if ('' !== $remaining) { |
| 98 |
throw new InvalidPropertyPathException(\sprintf('Could not parse property path "%s". Unexpected token "%s" at position %d.', $propertyPath, $remaining[0], $position)); |
| 99 |
} |
| 100 |
$this->length = \count($this->elements); |
| 101 |
} |
| 102 |
/** |
| 103 |
* {@inheritdoc} |
| 104 |
*/ |
| 105 |
public function __toString() |
| 106 |
{ |
| 107 |
return $this->pathAsString; |
| 108 |
} |
| 109 |
/** |
| 110 |
* {@inheritdoc} |
| 111 |
*/ |
| 112 |
public function getLength() |
| 113 |
{ |
| 114 |
return $this->length; |
| 115 |
} |
| 116 |
/** |
| 117 |
* {@inheritdoc} |
| 118 |
*/ |
| 119 |
public function getParent() |
| 120 |
{ |
| 121 |
if ($this->length <= 1) { |
| 122 |
return null; |
| 123 |
} |
| 124 |
$parent = clone $this; |
| 125 |
--$parent->length; |
| 126 |
$parent->pathAsString = \substr($parent->pathAsString, 0, \max(\strrpos($parent->pathAsString, '.'), \strrpos($parent->pathAsString, '['))); |
| 127 |
\array_pop($parent->elements); |
| 128 |
\array_pop($parent->isIndex); |
| 129 |
return $parent; |
| 130 |
} |
| 131 |
/** |
| 132 |
* Returns a new iterator for this path. |
| 133 |
* |
| 134 |
* @return PropertyPathIteratorInterface |
| 135 |
*/ |
| 136 |
#[\ReturnTypeWillChange] |
| 137 |
public function getIterator() |
| 138 |
{ |
| 139 |
return new PropertyPathIterator($this); |
| 140 |
} |
| 141 |
/** |
| 142 |
* {@inheritdoc} |
| 143 |
*/ |
| 144 |
public function getElements() |
| 145 |
{ |
| 146 |
return $this->elements; |
| 147 |
} |
| 148 |
/** |
| 149 |
* {@inheritdoc} |
| 150 |
*/ |
| 151 |
public function getElement(int $index) |
| 152 |
{ |
| 153 |
if (!isset($this->elements[$index])) { |
| 154 |
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the property path.', $index)); |
| 155 |
} |
| 156 |
return $this->elements[$index]; |
| 157 |
} |
| 158 |
/** |
| 159 |
* {@inheritdoc} |
| 160 |
*/ |
| 161 |
public function isProperty(int $index) |
| 162 |
{ |
| 163 |
if (!isset($this->isIndex[$index])) { |
| 164 |
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the property path.', $index)); |
| 165 |
} |
| 166 |
return !$this->isIndex[$index]; |
| 167 |
} |
| 168 |
/** |
| 169 |
* {@inheritdoc} |
| 170 |
*/ |
| 171 |
public function isIndex(int $index) |
| 172 |
{ |
| 173 |
if (!isset($this->isIndex[$index])) { |
| 174 |
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the property path.', $index)); |
| 175 |
} |
| 176 |
return $this->isIndex[$index]; |
| 177 |
} |
| 178 |
} |
| 179 |
|