123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace Symfony\Component\PropertyAccess;
- use Symfony\Component\PropertyAccess\Exception\OutOfBoundsException;
- class PropertyPathBuilder
- {
- private $elements = [];
- private $isIndex = [];
-
- public function __construct($path = null)
- {
- if (null !== $path) {
- $this->append($path);
- }
- }
-
- public function append($path, int $offset = 0, int $length = 0)
- {
- if (\is_string($path)) {
- $path = new PropertyPath($path);
- }
- if (0 === $length) {
- $end = $path->getLength();
- } else {
- $end = $offset + $length;
- }
- for (; $offset < $end; ++$offset) {
- $this->elements[] = $path->getElement($offset);
- $this->isIndex[] = $path->isIndex($offset);
- }
- }
-
- public function appendIndex(string $name)
- {
- $this->elements[] = $name;
- $this->isIndex[] = true;
- }
-
- public function appendProperty(string $name)
- {
- $this->elements[] = $name;
- $this->isIndex[] = false;
- }
-
- public function remove(int $offset, int $length = 1)
- {
- if (!isset($this->elements[$offset])) {
- throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
- }
- $this->resize($offset, $length, 0);
- }
-
- public function replace(int $offset, int $length, $path, int $pathOffset = 0, int $pathLength = 0)
- {
- if (\is_string($path)) {
- $path = new PropertyPath($path);
- }
- if ($offset < 0 && abs($offset) <= $this->getLength()) {
- $offset = $this->getLength() + $offset;
- } elseif (!isset($this->elements[$offset])) {
- throw new OutOfBoundsException('The offset '.$offset.' is not within the property path');
- }
- if (0 === $pathLength) {
- $pathLength = $path->getLength() - $pathOffset;
- }
- $this->resize($offset, $length, $pathLength);
- for ($i = 0; $i < $pathLength; ++$i) {
- $this->elements[$offset + $i] = $path->getElement($pathOffset + $i);
- $this->isIndex[$offset + $i] = $path->isIndex($pathOffset + $i);
- }
- ksort($this->elements);
- }
-
- public function replaceByIndex(int $offset, string $name = null)
- {
- if (!isset($this->elements[$offset])) {
- throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
- }
- if (null !== $name) {
- $this->elements[$offset] = $name;
- }
- $this->isIndex[$offset] = true;
- }
-
- public function replaceByProperty(int $offset, string $name = null)
- {
- if (!isset($this->elements[$offset])) {
- throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
- }
- if (null !== $name) {
- $this->elements[$offset] = $name;
- }
- $this->isIndex[$offset] = false;
- }
-
- public function getLength()
- {
- return \count($this->elements);
- }
-
- public function getPropertyPath()
- {
- $pathAsString = $this->__toString();
- return '' !== $pathAsString ? new PropertyPath($pathAsString) : null;
- }
-
- public function __toString()
- {
- $string = '';
- foreach ($this->elements as $offset => $element) {
- if ($this->isIndex[$offset]) {
- $element = '['.$element.']';
- } elseif ('' !== $string) {
- $string .= '.';
- }
- $string .= $element;
- }
- return $string;
- }
-
- private function resize(int $offset, int $cutLength, int $insertionLength)
- {
-
- if ($insertionLength === $cutLength) {
- return;
- }
- $length = \count($this->elements);
- if ($cutLength > $insertionLength) {
-
- $diff = $cutLength - $insertionLength;
- $newLength = $length - $diff;
-
-
-
-
- for ($i = $offset; $i < $newLength; ++$i) {
- $this->elements[$i] = $this->elements[$i + $diff];
- $this->isIndex[$i] = $this->isIndex[$i + $diff];
- }
-
- $this->elements = \array_slice($this->elements, 0, $i);
- $this->isIndex = \array_slice($this->isIndex, 0, $i);
- } else {
- $diff = $insertionLength - $cutLength;
- $newLength = $length + $diff;
- $indexAfterInsertion = $offset + $insertionLength;
-
-
-
-
-
-
-
-
-
-
- for ($i = max($length, $indexAfterInsertion); $i < $newLength; ++$i) {
- $this->elements[$i] = $this->elements[$i - $diff];
- $this->isIndex[$i] = $this->isIndex[$i - $diff];
- }
-
-
-
-
-
-
-
- for ($i = $length - 1; $i >= $indexAfterInsertion; --$i) {
- $this->elements[$i] = $this->elements[$i - $diff];
- $this->isIndex[$i] = $this->isIndex[$i - $diff];
- }
- }
- }
- }
|