No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PropertyPathBuilder.php 9.0KB

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