GenericMetadata.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * @return $this
  111. *
  112. * @throws ConstraintDefinitionException When trying to add the
  113. * {@link Traverse} constraint
  114. */
  115. public function addConstraint(Constraint $constraint)
  116. {
  117. if ($constraint instanceof Traverse) {
  118. throw new ConstraintDefinitionException(sprintf('The constraint "%s" can only be put on classes. Please use "Symfony\Component\Validator\Constraints\Valid" instead.', \get_class($constraint)));
  119. }
  120. if ($constraint instanceof Valid) {
  121. $this->cascadingStrategy = CascadingStrategy::CASCADE;
  122. if ($constraint->traverse) {
  123. // Traverse unless the value is not traversable
  124. $this->traversalStrategy = TraversalStrategy::IMPLICIT;
  125. if (!$constraint->deep) {
  126. $this->traversalStrategy |= TraversalStrategy::STOP_RECURSION;
  127. }
  128. } else {
  129. $this->traversalStrategy = TraversalStrategy::NONE;
  130. }
  131. return $this;
  132. }
  133. $this->constraints[] = $constraint;
  134. foreach ($constraint->groups as $group) {
  135. $this->constraintsByGroup[$group][] = $constraint;
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Adds an list of constraints.
  141. *
  142. * @param Constraint[] $constraints The constraints to add
  143. *
  144. * @return $this
  145. */
  146. public function addConstraints(array $constraints)
  147. {
  148. foreach ($constraints as $constraint) {
  149. $this->addConstraint($constraint);
  150. }
  151. return $this;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getConstraints()
  157. {
  158. return $this->constraints;
  159. }
  160. /**
  161. * Returns whether this element has any constraints.
  162. *
  163. * @return bool
  164. */
  165. public function hasConstraints()
  166. {
  167. return \count($this->constraints) > 0;
  168. }
  169. /**
  170. * {@inheritdoc}
  171. *
  172. * Aware of the global group (* group).
  173. */
  174. public function findConstraints($group)
  175. {
  176. return isset($this->constraintsByGroup[$group])
  177. ? $this->constraintsByGroup[$group]
  178. : array();
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function getCascadingStrategy()
  184. {
  185. return $this->cascadingStrategy;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getTraversalStrategy()
  191. {
  192. return $this->traversalStrategy;
  193. }
  194. /**
  195. * Exists for compatibility with the deprecated
  196. * {@link Symfony\Component\Validator\MetadataInterface}.
  197. *
  198. * Should not be used.
  199. *
  200. * Implemented for backward compatibility with Symfony < 2.5.
  201. *
  202. * @throws BadMethodCallException
  203. *
  204. * @deprecated since version 2.5, to be removed in 3.0.
  205. */
  206. public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath)
  207. {
  208. throw new BadMethodCallException('Not supported.');
  209. }
  210. }