Debian Codegraph @

lib/3rdParty/composer/symfony/property-access/PropertyPath.php

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