/src/Symfony/Component/PropertyAccess/PropertyPathBuilder.php

https://github.com/gimler/symfony · PHP · 299 lines · 138 code · 40 blank · 121 comment · 23 complexity · bd561a5ed7694d01018aa88f86acafb9 MD5 · raw file

  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 = array();
  18. private $isIndex = array();
  19. /**
  20. * Creates a new property path builder.
  21. *
  22. * @param null|PropertyPathInterface|string $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, $offset = 0, $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($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($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($offset, $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($offset, $length, $path, $pathOffset = 0, $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($offset, $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($offset, $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 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. * @param int $offset The offset where the removed chunk starts
  202. * @param int $cutLength The length of the removed chunk
  203. * @param int $insertionLength The length of the inserted chunk
  204. */
  205. private function resize($offset, $cutLength, $insertionLength)
  206. {
  207. // Nothing else to do in this case
  208. if ($insertionLength === $cutLength) {
  209. return;
  210. }
  211. $length = \count($this->elements);
  212. if ($cutLength > $insertionLength) {
  213. // More elements should be removed than inserted
  214. $diff = $cutLength - $insertionLength;
  215. $newLength = $length - $diff;
  216. // Shift elements to the left (left-to-right until the new end)
  217. // Max allowed offset to be shifted is such that
  218. // $offset + $diff < $length (otherwise invalid index access)
  219. // i.e. $offset < $length - $diff = $newLength
  220. for ($i = $offset; $i < $newLength; ++$i) {
  221. $this->elements[$i] = $this->elements[$i + $diff];
  222. $this->isIndex[$i] = $this->isIndex[$i + $diff];
  223. }
  224. // All remaining elements should be removed
  225. for (; $i < $length; ++$i) {
  226. unset($this->elements[$i], $this->isIndex[$i]);
  227. }
  228. } else {
  229. $diff = $insertionLength - $cutLength;
  230. $newLength = $length + $diff;
  231. $indexAfterInsertion = $offset + $insertionLength;
  232. // $diff <= $insertionLength
  233. // $indexAfterInsertion >= $insertionLength
  234. // => $diff <= $indexAfterInsertion
  235. // In each of the following loops, $i >= $diff must hold,
  236. // otherwise ($i - $diff) becomes negative.
  237. // Shift old elements to the right to make up space for the
  238. // inserted elements. This needs to be done left-to-right in
  239. // order to preserve an ascending array index order
  240. // Since $i = max($length, $indexAfterInsertion) and $indexAfterInsertion >= $diff,
  241. // $i >= $diff is guaranteed.
  242. for ($i = max($length, $indexAfterInsertion); $i < $newLength; ++$i) {
  243. $this->elements[$i] = $this->elements[$i - $diff];
  244. $this->isIndex[$i] = $this->isIndex[$i - $diff];
  245. }
  246. // Shift remaining elements to the right. Do this right-to-left
  247. // so we don't overwrite elements before copying them
  248. // The last written index is the immediate index after the inserted
  249. // string, because the indices before that will be overwritten
  250. // anyway.
  251. // Since $i >= $indexAfterInsertion and $indexAfterInsertion >= $diff,
  252. // $i >= $diff is guaranteed.
  253. for ($i = $length - 1; $i >= $indexAfterInsertion; --$i) {
  254. $this->elements[$i] = $this->elements[$i - $diff];
  255. $this->isIndex[$i] = $this->isIndex[$i - $diff];
  256. }
  257. }
  258. }
  259. }