Debian Codegraph @

lib/3rdParty/composer/symfony/property-access/PropertyPathBuilder.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\OutOfBoundsException;
15
16 /**
17 * @author Bernhard Schussek <[email protected]>
18 */
19 class PropertyPathBuilder
20 {
21 private array $elements = [];
22 private array $isIndex = [];
23
24 public function __construct(PropertyPathInterface|string|null $path = null)
25 {
26 if (null !== $path) {
27 $this->append($path);
28 }
29 }
30
31 /**
32 * Appends a (sub-) path to the current path.
33 *
34 * @param int $offset The offset where the appended piece starts in $path
35 * @param int $length The length of the appended piece; if 0, the full path is appended
36 */
37 public function append(PropertyPathInterface|string $path, int $offset = 0, int $length = 0): void
38 {
39 if (\is_string($path)) {
40 $path = new PropertyPath($path);
41 }
42
43 if (0 === $length) {
44 $end = $path->getLength();
45 } else {
46 $end = $offset + $length;
47 }
48
49 for (; $offset < $end; ++$offset) {
50 $this->elements[] = $path->getElement($offset);
51 $this->isIndex[] = $path->isIndex($offset);
52 }
53 }
54
55 /**
56 * Appends an index element to the current path.
57 */
58 public function appendIndex(string $name): void
59 {
60 $this->elements[] = $name;
61 $this->isIndex[] = true;
62 }
63
64 /**
65 * Appends a property element to the current path.
66 */
67 public function appendProperty(string $name): void
68 {
69 $this->elements[] = $name;
70 $this->isIndex[] = false;
71 }
72
73 /**
74 * Removes elements from the current path.
75 *
76 * @throws OutOfBoundsException if offset is invalid
77 */
78 public function remove(int $offset, int $length = 1): void
79 {
80 if (!isset($this->elements[$offset])) {
81 throw new OutOfBoundsException(\sprintf('The offset "%s" is not within the property path.', $offset));
82 }
83
84 $this->resize($offset, $length, 0);
85 }
86
87 /**
88 * Replaces a sub-path by a different (sub-) path.
89 *
90 * @param int $pathOffset The offset where the inserted piece starts in $path
91 * @param int $pathLength The length of the inserted piece; if 0, the full path is inserted
92 *
93 * @throws OutOfBoundsException If the offset is invalid
94 */
95 public function replace(int $offset, int $length, PropertyPathInterface|string $path, int $pathOffset = 0, int $pathLength = 0): void
96 {
97 if (\is_string($path)) {
98 $path = new PropertyPath($path);
99 }
100
101 if ($offset < 0 && abs($offset) <= $this->getLength()) {
102 $offset = $this->getLength() + $offset;
103 } elseif (!isset($this->elements[$offset])) {
104 throw new OutOfBoundsException('The offset '.$offset.' is not within the property path');
105 }
106
107 if (0 === $pathLength) {
108 $pathLength = $path->getLength() - $pathOffset;
109 }
110
111 $this->resize($offset, $length, $pathLength);
112
113 for ($i = 0; $i < $pathLength; ++$i) {
114 $this->elements[$offset + $i] = $path->getElement($pathOffset + $i);
115 $this->isIndex[$offset + $i] = $path->isIndex($pathOffset + $i);
116 }
117 ksort($this->elements);
118 }
119
120 /**
121 * Replaces a property element by an index element.
122 *
123 * @throws OutOfBoundsException If the offset is invalid
124 */
125 public function replaceByIndex(int $offset, ?string $name = null): void
126 {
127 if (!isset($this->elements[$offset])) {
128 throw new OutOfBoundsException(\sprintf('The offset "%s" is not within the property path.', $offset));
129 }
130
131 if (null !== $name) {
132 $this->elements[$offset] = $name;
133 }
134
135 $this->isIndex[$offset] = true;
136 }
137
138 /**
139 * Replaces an index element by a property element.
140 *
141 * @throws OutOfBoundsException If the offset is invalid
142 */
143 public function replaceByProperty(int $offset, ?string $name = null): void
144 {
145 if (!isset($this->elements[$offset])) {
146 throw new OutOfBoundsException(\sprintf('The offset "%s" is not within the property path.', $offset));
147 }
148
149 if (null !== $name) {
150 $this->elements[$offset] = $name;
151 }
152
153 $this->isIndex[$offset] = false;
154 }
155
156 /**
157 * Returns the length of the current path.
158 */
159 public function getLength(): int
160 {
161 return \count($this->elements);
162 }
163
164 /**
165 * Returns the current property path.
166 */
167 public function getPropertyPath(): ?PropertyPathInterface
168 {
169 $pathAsString = $this->__toString();
170
171 return '' !== $pathAsString ? new PropertyPath($pathAsString) : null;
172 }
173
174 /**
175 * Returns the current property path as string.
176 */
177 public function __toString(): string
178 {
179 $string = '';
180
181 foreach ($this->elements as $offset => $element) {
182 if ($this->isIndex[$offset]) {
183 $element = '['.$element.']';
184 } elseif ('' !== $string) {
185 $string .= '.';
186 }
187
188 $string .= $element;
189 }
190
191 return $string;
192 }
193
194 /**
195 * Resizes the path so that a chunk of length $cutLength is
196 * removed at $offset and another chunk of length $insertionLength
197 * can be inserted.
198 */
199 private function resize(int $offset, int $cutLength, int $insertionLength): void
200 {
201 // Nothing else to do in this case
202 if ($insertionLength === $cutLength) {
203 return;
204 }
205
206 $length = \count($this->elements);
207
208 if ($cutLength > $insertionLength) {
209 // More elements should be removed than inserted
210 $diff = $cutLength - $insertionLength;
211 $newLength = $length - $diff;
212
213 // Shift elements to the left (left-to-right until the new end)
214 // Max allowed offset to be shifted is such that
215 // $offset + $diff < $length (otherwise invalid index access)
216 // i.e. $offset < $length - $diff = $newLength
217 for ($i = $offset; $i < $newLength; ++$i) {
218 $this->elements[$i] = $this->elements[$i + $diff];
219 $this->isIndex[$i] = $this->isIndex[$i + $diff];
220 }
221
222 // All remaining elements should be removed
223 $this->elements = \array_slice($this->elements, 0, $i);
224 $this->isIndex = \array_slice($this->isIndex, 0, $i);
225 } else {
226 $diff = $insertionLength - $cutLength;
227
228 $newLength = $length + $diff;
229 $indexAfterInsertion = $offset + $insertionLength;
230
231 // $diff <= $insertionLength
232 // $indexAfterInsertion >= $insertionLength
233 // => $diff <= $indexAfterInsertion
234
235 // In each of the following loops, $i >= $diff must hold,
236 // otherwise ($i - $diff) becomes negative.
237
238 // Shift old elements to the right to make up space for the
239 // inserted elements. This needs to be done left-to-right in
240 // order to preserve an ascending array index order
241 // Since $i = max($length, $indexAfterInsertion) and $indexAfterInsertion >= $diff,
242 // $i >= $diff is guaranteed.
243 for ($i = max($length, $indexAfterInsertion); $i < $newLength; ++$i) {
244 $this->elements[$i] = $this->elements[$i - $diff];
245 $this->isIndex[$i] = $this->isIndex[$i - $diff];
246 }
247
248 // Shift remaining elements to the right. Do this right-to-left
249 // so we don't overwrite elements before copying them
250 // The last written index is the immediate index after the inserted
251 // string, because the indices before that will be overwritten
252 // anyway.
253 // Since $i >= $indexAfterInsertion and $indexAfterInsertion >= $diff,
254 // $i >= $diff is guaranteed.
255 for ($i = $length - 1; $i >= $indexAfterInsertion; --$i) {
256 $this->elements[$i] = $this->elements[$i - $diff];
257 $this->isIndex[$i] = $this->isIndex[$i - $diff];
258 }
259 }
260 }
261 }