| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class PropertyPath implements \IteratorAggregate, PropertyPathInterface |
| 26 |
{ |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public const SINGULAR_SEPARATOR = '|'; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private array $elements = []; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private int $length; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private array $isIndex = []; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private array $isNullSafe = []; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private string $pathAsString; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function __construct(self|string $propertyPath) |
| 72 |
{ |
| 73 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |