Drupal investigation

GenericMetadata.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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\Validator\Mapping;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Constraints\Traverse;
  13. use Symfony\Component\Validator\Constraints\Valid;
  14. use Symfony\Component\Validator\Exception\BadMethodCallException;
  15. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  16. use Symfony\Component\Validator\ValidationVisitorInterface;
  17. /**
  18. * A generic container of {@link Constraint} objects.
  19. *
  20. * This class supports serialization and cloning.
  21. *
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. */
  24. class GenericMetadata implements MetadataInterface
  25. {
  26. /**
  27. * @var Constraint[]
  28. *
  29. * @internal This property is public in order to reduce the size of the
  30. * class' serialized representation. Do not access it. Use
  31. * {@link getConstraints()} and {@link findConstraints()} instead.
  32. */
  33. public $constraints = array();
  34. /**
  35. * @var array
  36. *
  37. * @internal This property is public in order to reduce the size of the
  38. * class' serialized representation. Do not access it. Use
  39. * {@link findConstraints()} instead.
  40. */
  41. public $constraintsByGroup = array();
  42. /**
  43. * The strategy for cascading objects.
  44. *
  45. * By default, objects are not cascaded.
  46. *
  47. * @var int
  48. *
  49. * @see CascadingStrategy
  50. *
  51. * @internal This property is public in order to reduce the size of the
  52. * class' serialized representation. Do not access it. Use
  53. * {@link getCascadingStrategy()} instead.
  54. */
  55. public $cascadingStrategy = CascadingStrategy::NONE;
  56. /**
  57. * The strategy for traversing traversable objects.
  58. *
  59. * By default, traversable objects are not traversed.
  60. *
  61. * @var int
  62. *
  63. * @see TraversalStrategy
  64. *
  65. * @internal This property is public in order to reduce the size of the
  66. * class' serialized representation. Do not access it. Use
  67. * {@link getTraversalStrategy()} instead.
  68. */
  69. public $traversalStrategy = TraversalStrategy::NONE;
  70. /**
  71. * Returns the names of the properties that should be serialized.
  72. *
  73. * @return string[]
  74. */
  75. public function __sleep()
  76. {
  77. return array(
  78. 'constraints',
  79. 'constraintsByGroup',
  80. 'cascadingStrategy',
  81. 'traversalStrategy',
  82. );
  83. }
  84. /**
  85. * Clones this object.
  86. */
  87. public function __clone()
  88. {
  89. $constraints = $this->constraints;
  90. $this->constraints = array();
  91. $this->constraintsByGroup = array();
  92. foreach ($constraints as $constraint) {
  93. $this->addConstraint(clone $constraint);
  94. }
  95. }
  96. /**
  97. * Adds a constraint.
  98. *
  99. * If the constraint {@link Valid} is added, the cascading strategy will be
  100. * changed to {@link CascadingStrategy::CASCADE}. Depending on the
  101. * properties $traverse and $deep of that constraint, the traversal strategy
  102. * will be set to one of the following:
  103. *
  104. * - {@link TraversalStrategy::IMPLICIT} if $traverse is enabled and $deep
  105. * is enabled
  106. * - {@link TraversalStrategy::IMPLICIT} | {@link TraversalStrategy::STOP_RECURSION}
  107. * if $traverse is enabled, but $deep is disabled
  108. * - {@link TraversalStrategy::NONE} if $traverse is disabled
  109. *
  110. * @param Constraint $constraint The constraint to add
  111. *
  112. * @return $this
  113. *
  114. * @throws ConstraintDefinitionException When trying to add the
  115. * {@link Traverse} constraint
  116. */
  117. public function addConstraint(Constraint $constraint)
  118. {
  119. if ($constraint instanceof Traverse) {
  120. throw new ConstraintDefinitionException(sprintf(
  121. 'The constraint "%s" can only be put on classes. Please use '.
  122. '"Symfony\Component\Validator\Constraints\Valid" instead.',
  123. get_class($constraint)
  124. ));
  125. }
  126. if ($constraint instanceof Valid) {
  127. $this->cascadingStrategy = CascadingStrategy::CASCADE;
  128. if ($constraint->traverse) {
  129. // Traverse unless the value is not traversable
  130. $this->traversalStrategy = TraversalStrategy::IMPLICIT;
  131. if (!$constraint->deep) {
  132. $this->traversalStrategy |= TraversalStrategy::STOP_RECURSION;
  133. }
  134. } else {
  135. $this->traversalStrategy = TraversalStrategy::NONE;
  136. }
  137. return $this;
  138. }
  139. $this->constraints[] = $constraint;
  140. foreach ($constraint->groups as $group) {
  141. $this->constraintsByGroup[$group][] = $constraint;
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Adds an list of constraints.
  147. *
  148. * @param Constraint[] $constraints The constraints to add
  149. *
  150. * @return $this
  151. */
  152. public function addConstraints(array $constraints)
  153. {
  154. foreach ($constraints as $constraint) {
  155. $this->addConstraint($constraint);
  156. }
  157. return $this;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function getConstraints()
  163. {
  164. return $this->constraints;
  165. }
  166. /**
  167. * Returns whether this element has any constraints.
  168. *
  169. * @return bool
  170. */
  171. public function hasConstraints()
  172. {
  173. return count($this->constraints) > 0;
  174. }
  175. /**
  176. * {@inheritdoc}
  177. *
  178. * Aware of the global group (* group).
  179. */
  180. public function findConstraints($group)
  181. {
  182. return isset($this->constraintsByGroup[$group])
  183. ? $this->constraintsByGroup[$group]
  184. : array();
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function getCascadingStrategy()
  190. {
  191. return $this->cascadingStrategy;
  192. }
  193. /**
  194. * {@inheritdoc}
  195. */
  196. public function getTraversalStrategy()
  197. {
  198. return $this->traversalStrategy;
  199. }
  200. /**
  201. * Exists for compatibility with the deprecated
  202. * {@link Symfony\Component\Validator\MetadataInterface}.
  203. *
  204. * Should not be used.
  205. *
  206. * Implemented for backward compatibility with Symfony < 2.5.
  207. *
  208. * @throws BadMethodCallException
  209. *
  210. * @deprecated since version 2.5, to be removed in 3.0.
  211. */
  212. public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath)
  213. {
  214. throw new BadMethodCallException('Not supported.');
  215. }
  216. }