PropertyPathBuilder.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. * @param string $name The name of the appended index
  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. * @param string $name The name of the appended property
  69. */
  70. public function appendProperty(string $name)
  71. {
  72. $this->elements[] = $name;
  73. $this->isIndex[] = false;
  74. }
  75. /**
  76. * Removes elements from the current path.
  77. *
  78. * @param int $offset The offset at which to remove
  79. * @param int $length The length of the removed piece
  80. *
  81. * @throws OutOfBoundsException if offset is invalid
  82. */
  83. public function remove(int $offset, int $length = 1)
  84. {
  85. if (!isset($this->elements[$offset])) {
  86. throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
  87. }
  88. $this->resize($offset, $length, 0);
  89. }
  90. /**
  91. * Replaces a sub-path by a different (sub-) path.
  92. *
  93. * @param int $offset The offset at which to replace
  94. * @param int $length The length of the piece to replace
  95. * @param PropertyPathInterface|string $path The path to insert
  96. * @param int $pathOffset The offset where the inserted piece
  97. * starts in $path
  98. * @param int $pathLength The length of the inserted piece
  99. * If 0, the full path is inserted
  100. *
  101. * @throws OutOfBoundsException If the offset is invalid
  102. */
  103. public function replace(int $offset, int $length, $path, int $pathOffset = 0, int $pathLength = 0)
  104. {
  105. if (\is_string($path)) {
  106. $path = new PropertyPath($path);
  107. }
  108. if ($offset < 0 && abs($offset) <= $this->getLength()) {
  109. $offset = $this->getLength() + $offset;
  110. } elseif (!isset($this->elements[$offset])) {
  111. throw new OutOfBoundsException('The offset '.$offset.' is not within the property path');
  112. }
  113. if (0 === $pathLength) {
  114. $pathLength = $path->getLength() - $pathOffset;
  115. }
  116. $this->resize($offset, $length, $pathLength);
  117. for ($i = 0; $i < $pathLength; ++$i) {
  118. $this->elements[$offset + $i] = $path->getElement($pathOffset + $i);
  119. $this->isIndex[$offset + $i] = $path->isIndex($pathOffset + $i);
  120. }
  121. ksort($this->elements);
  122. }
  123. /**
  124. * Replaces a property element by an index element.
  125. *
  126. * @param int $offset The offset at which to replace
  127. * @param string $name The new name of the element. Optional
  128. *
  129. * @throws OutOfBoundsException If the offset is invalid
  130. */
  131. public function replaceByIndex(int $offset, string $name = null)
  132. {
  133. if (!isset($this->elements[$offset])) {
  134. throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
  135. }
  136. if (null !== $name) {
  137. $this->elements[$offset] = $name;
  138. }
  139. $this->isIndex[$offset] = true;
  140. }
  141. /**
  142. * Replaces an index element by a property element.
  143. *
  144. * @param int $offset The offset at which to replace
  145. * @param string $name The new name of the element. Optional
  146. *
  147. * @throws OutOfBoundsException If the offset is invalid
  148. */
  149. public function replaceByProperty(int $offset, string $name = null)
  150. {
  151. if (!isset($this->elements[$offset])) {
  152. throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
  153. }
  154. if (null !== $name) {
  155. $this->elements[$offset] = $name;
  156. }
  157. $this->isIndex[$offset] = false;
  158. }
  159. /**
  160. * Returns the length of the current path.
  161. *
  162. * @return int The path length
  163. */
  164. public function getLength()
  165. {
  166. return \count($this->elements);
  167. }
  168. /**
  169. * Returns the current property path.
  170. *
  171. * @return PropertyPathInterface|null The constructed property path
  172. */
  173. public function getPropertyPath()
  174. {
  175. $pathAsString = $this->__toString();
  176. return '' !== $pathAsString ? new PropertyPath($pathAsString) : null;
  177. }
  178. /**
  179. * Returns the current property path as string.
  180. *
  181. * @return string The property path as string
  182. */
  183. public function __toString()
  184. {
  185. $string = '';
  186. foreach ($this->elements as $offset => $element) {
  187. if ($this->isIndex[$offset]) {
  188. $element = '['.$element.']';
  189. } elseif ('' !== $string) {
  190. $string .= '.';
  191. }
  192. $string .= $element;
  193. }
  194. return $string;
  195. }
  196. /**
  197. * Resizes the path so that a chunk of length $cutLength is
  198. * removed at $offset and another chunk of length $insertionLength
  199. * can be inserted.
  200. */
  201. private function resize(int $offset, int $cutLength, int $insertionLength)
  202. {
  203. // Nothing else to do in this case
  204. if ($insertionLength === $cutLength) {
  205. return;
  206. }
  207. $length = \count($this->elements);
  208. if ($cutLength > $insertionLength) {
  209. // More elements should be removed than inserted
  210. $diff = $cutLength - $insertionLength;
  211. $newLength = $length - $diff;
  212. // Shift elements to the left (left-to-right until the new end)
  213. // Max allowed offset to be shifted is such that
  214. // $offset + $diff < $length (otherwise invalid index access)
  215. // i.e. $offset < $length - $diff = $newLength
  216. for ($i = $offset; $i < $newLength; ++$i) {
  217. $this->elements[$i] = $this->elements[$i + $diff];
  218. $this->isIndex[$i] = $this->isIndex[$i + $diff];
  219. }
  220. // All remaining elements should be removed
  221. $this->elements = \array_slice($this->elements, 0, $i);
  222. $this->isIndex = \array_slice($this->isIndex, 0, $i);
  223. } else {
  224. $diff = $insertionLength - $cutLength;
  225. $newLength = $length + $diff;
  226. $indexAfterInsertion = $offset + $insertionLength;
  227. // $diff <= $insertionLength
  228. // $indexAfterInsertion >= $insertionLength
  229. // => $diff <= $indexAfterInsertion
  230. // In each of the following loops, $i >= $diff must hold,
  231. // otherwise ($i - $diff) becomes negative.
  232. // Shift old elements to the right to make up space for the
  233. // inserted elements. This needs to be done left-to-right in
  234. // order to preserve an ascending array index order
  235. // Since $i = max($length, $indexAfterInsertion) and $indexAfterInsertion >= $diff,
  236. // $i >= $diff is guaranteed.
  237. for ($i = max($length, $indexAfterInsertion); $i < $newLength; ++$i) {
  238. $this->elements[$i] = $this->elements[$i - $diff];
  239. $this->isIndex[$i] = $this->isIndex[$i - $diff];
  240. }
  241. // Shift remaining elements to the right. Do this right-to-left
  242. // so we don't overwrite elements before copying them
  243. // The last written index is the immediate index after the inserted
  244. // string, because the indices before that will be overwritten
  245. // anyway.
  246. // Since $i >= $indexAfterInsertion and $indexAfterInsertion >= $diff,
  247. // $i >= $diff is guaranteed.
  248. for ($i = $length - 1; $i >= $indexAfterInsertion; --$i) {
  249. $this->elements[$i] = $this->elements[$i - $diff];
  250. $this->isIndex[$i] = $this->isIndex[$i - $diff];
  251. }
  252. }
  253. }
  254. }