GenericMetadata.php 6.7 KB

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