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