PropertyPath.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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\InvalidArgumentException;
  12. use Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException;
  13. use Symfony\Component\PropertyAccess\Exception\OutOfBoundsException;
  14. /**
  15. * Default implementation of {@link PropertyPathInterface}.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class PropertyPath implements \IteratorAggregate, PropertyPathInterface
  20. {
  21. /**
  22. * Character used for separating between plural and singular of an element.
  23. */
  24. const SINGULAR_SEPARATOR = '|';
  25. /**
  26. * The elements of the property path.
  27. *
  28. * @var array
  29. */
  30. private $elements = array();
  31. /**
  32. * The number of elements in the property path.
  33. *
  34. * @var int
  35. */
  36. private $length;
  37. /**
  38. * Contains a Boolean for each property in $elements denoting whether this
  39. * element is an index. It is a property otherwise.
  40. *
  41. * @var array
  42. */
  43. private $isIndex = array();
  44. /**
  45. * String representation of the path.
  46. *
  47. * @var string
  48. */
  49. private $pathAsString;
  50. /**
  51. * Constructs a property path from a string.
  52. *
  53. * @param PropertyPath|string $propertyPath The property path as string or instance
  54. *
  55. * @throws InvalidArgumentException If the given path is not a string
  56. * @throws InvalidPropertyPathException If the syntax of the property path is not valid
  57. */
  58. public function __construct($propertyPath)
  59. {
  60. // Can be used as copy constructor
  61. if ($propertyPath instanceof self) {
  62. /* @var PropertyPath $propertyPath */
  63. $this->elements = $propertyPath->elements;
  64. $this->length = $propertyPath->length;
  65. $this->isIndex = $propertyPath->isIndex;
  66. $this->pathAsString = $propertyPath->pathAsString;
  67. return;
  68. }
  69. if (!is_string($propertyPath)) {
  70. throw new InvalidArgumentException(sprintf(
  71. 'The property path constructor needs a string or an instance of '.
  72. '"Symfony\Component\PropertyAccess\PropertyPath". '.
  73. 'Got: "%s"',
  74. is_object($propertyPath) ? get_class($propertyPath) : gettype($propertyPath)
  75. ));
  76. }
  77. if ('' === $propertyPath) {
  78. throw new InvalidPropertyPathException('The property path should not be empty.');
  79. }
  80. $this->pathAsString = $propertyPath;
  81. $position = 0;
  82. $remaining = $propertyPath;
  83. // first element is evaluated differently - no leading dot for properties
  84. $pattern = '/^(([^\.\[]++)|\[([^\]]++)\])(.*)/';
  85. while (preg_match($pattern, $remaining, $matches)) {
  86. if ('' !== $matches[2]) {
  87. $element = $matches[2];
  88. $this->isIndex[] = false;
  89. } else {
  90. $element = $matches[3];
  91. $this->isIndex[] = true;
  92. }
  93. $this->elements[] = $element;
  94. $position += strlen($matches[1]);
  95. $remaining = $matches[4];
  96. $pattern = '/^(\.([^\.|\[]++)|\[([^\]]++)\])(.*)/';
  97. }
  98. if ('' !== $remaining) {
  99. throw new InvalidPropertyPathException(sprintf(
  100. 'Could not parse property path "%s". Unexpected token "%s" at position %d',
  101. $propertyPath,
  102. $remaining[0],
  103. $position
  104. ));
  105. }
  106. $this->length = count($this->elements);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function __toString()
  112. {
  113. return $this->pathAsString;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getLength()
  119. {
  120. return $this->length;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getParent()
  126. {
  127. if ($this->length <= 1) {
  128. return;
  129. }
  130. $parent = clone $this;
  131. --$parent->length;
  132. $parent->pathAsString = substr($parent->pathAsString, 0, max(strrpos($parent->pathAsString, '.'), strrpos($parent->pathAsString, '[')));
  133. array_pop($parent->elements);
  134. array_pop($parent->isIndex);
  135. return $parent;
  136. }
  137. /**
  138. * Returns a new iterator for this path.
  139. *
  140. * @return PropertyPathIteratorInterface
  141. */
  142. public function getIterator()
  143. {
  144. return new PropertyPathIterator($this);
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function getElements()
  150. {
  151. return $this->elements;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getElement($index)
  157. {
  158. if (!isset($this->elements[$index])) {
  159. throw new OutOfBoundsException(sprintf('The index %s is not within the property path', $index));
  160. }
  161. return $this->elements[$index];
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function isProperty($index)
  167. {
  168. if (!isset($this->isIndex[$index])) {
  169. throw new OutOfBoundsException(sprintf('The index %s is not within the property path', $index));
  170. }
  171. return !$this->isIndex[$index];
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function isIndex($index)
  177. {
  178. if (!isset($this->isIndex[$index])) {
  179. throw new OutOfBoundsException(sprintf('The index %s is not within the property path', $index));
  180. }
  181. return $this->isIndex[$index];
  182. }
  183. }