PropertyPath.php 5.6 KB

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