| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\PropertyAccess; |
| 13 |
|
| 14 |
use Symfony\Component\PropertyAccess\Exception\OutOfBoundsException; |
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 57 |
|
| 58 |
public function appendIndex(string $name): void |
| 59 |
{ |
| 60 |
$this->elements[] = $name; |
| 61 |
$this->isIndex[] = true; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function appendProperty(string $name): void |
| 68 |
{ |
| 69 |
$this->elements[] = $name; |
| 70 |
$this->isIndex[] = false; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 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 |
|
| 122 |
|
| 123 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 158 |
|
| 159 |
public function getLength(): int |
| 160 |
{ |
| 161 |
return \count($this->elements); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function getPropertyPath(): ?PropertyPathInterface |
| 168 |
{ |
| 169 |
$pathAsString = $this->__toString(); |
| 170 |
|
| 171 |
return '' !== $pathAsString ? new PropertyPath($pathAsString) : null; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 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 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private function resize(int $offset, int $cutLength, int $insertionLength): void |
| 200 |
{ |
| 201 |
|
| 202 |
if ($insertionLength === $cutLength) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
$length = \count($this->elements); |
| 207 |
|
| 208 |
if ($cutLength > $insertionLength) { |
| 209 |
|
| 210 |
$diff = $cutLength - $insertionLength; |
| 211 |
$newLength = $length - $diff; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 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 |
|
| 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 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 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 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 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 |
} |