4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Mapping;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Constraints\Traverse;
16 use Symfony\Component\Validator\Constraints\Valid;
17 use Symfony\Component\Validator\Exception\BadMethodCallException;
18 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
19 use Symfony\Component\Validator\ValidationVisitorInterface;
22 * A generic container of {@link Constraint} objects.
24 * This class supports serialization and cloning.
26 * @author Bernhard Schussek <bschussek@gmail.com>
28 class GenericMetadata implements MetadataInterface
33 * @internal This property is public in order to reduce the size of the
34 * class' serialized representation. Do not access it. Use
35 * {@link getConstraints()} and {@link findConstraints()} instead.
37 public $constraints = array();
42 * @internal This property is public in order to reduce the size of the
43 * class' serialized representation. Do not access it. Use
44 * {@link findConstraints()} instead.
46 public $constraintsByGroup = array();
49 * The strategy for cascading objects.
51 * By default, objects are not cascaded.
55 * @see CascadingStrategy
57 * @internal This property is public in order to reduce the size of the
58 * class' serialized representation. Do not access it. Use
59 * {@link getCascadingStrategy()} instead.
61 public $cascadingStrategy = CascadingStrategy::NONE;
64 * The strategy for traversing traversable objects.
66 * By default, traversable objects are not traversed.
70 * @see TraversalStrategy
72 * @internal This property is public in order to reduce the size of the
73 * class' serialized representation. Do not access it. Use
74 * {@link getTraversalStrategy()} instead.
76 public $traversalStrategy = TraversalStrategy::NONE;
79 * Returns the names of the properties that should be serialized.
83 public function __sleep()
96 public function __clone()
98 $constraints = $this->constraints;
100 $this->constraints = array();
101 $this->constraintsByGroup = array();
103 foreach ($constraints as $constraint) {
104 $this->addConstraint(clone $constraint);
111 * If the constraint {@link Valid} is added, the cascading strategy will be
112 * changed to {@link CascadingStrategy::CASCADE}. Depending on the
113 * properties $traverse and $deep of that constraint, the traversal strategy
114 * will be set to one of the following:
116 * - {@link TraversalStrategy::IMPLICIT} if $traverse is enabled and $deep
118 * - {@link TraversalStrategy::IMPLICIT} | {@link TraversalStrategy::STOP_RECURSION}
119 * if $traverse is enabled, but $deep is disabled
120 * - {@link TraversalStrategy::NONE} if $traverse is disabled
122 * @param Constraint $constraint The constraint to add
126 * @throws ConstraintDefinitionException When trying to add the
127 * {@link Traverse} constraint
129 public function addConstraint(Constraint $constraint)
131 if ($constraint instanceof Traverse) {
132 throw new ConstraintDefinitionException(sprintf(
133 'The constraint "%s" can only be put on classes. Please use '.
134 '"Symfony\Component\Validator\Constraints\Valid" instead.',
135 get_class($constraint)
139 if ($constraint instanceof Valid) {
140 $this->cascadingStrategy = CascadingStrategy::CASCADE;
142 if ($constraint->traverse) {
143 // Traverse unless the value is not traversable
144 $this->traversalStrategy = TraversalStrategy::IMPLICIT;
146 if (!$constraint->deep) {
147 $this->traversalStrategy |= TraversalStrategy::STOP_RECURSION;
150 $this->traversalStrategy = TraversalStrategy::NONE;
156 $this->constraints[] = $constraint;
158 foreach ($constraint->groups as $group) {
159 $this->constraintsByGroup[$group][] = $constraint;
166 * Adds an list of constraints.
168 * @param Constraint[] $constraints The constraints to add
172 public function addConstraints(array $constraints)
174 foreach ($constraints as $constraint) {
175 $this->addConstraint($constraint);
184 public function getConstraints()
186 return $this->constraints;
190 * Returns whether this element has any constraints.
194 public function hasConstraints()
196 return count($this->constraints) > 0;
202 * Aware of the global group (* group).
204 public function findConstraints($group)
206 return isset($this->constraintsByGroup[$group])
207 ? $this->constraintsByGroup[$group]
214 public function getCascadingStrategy()
216 return $this->cascadingStrategy;
222 public function getTraversalStrategy()
224 return $this->traversalStrategy;
228 * Exists for compatibility with the deprecated
229 * {@link Symfony\Component\Validator\MetadataInterface}.
231 * Should not be used.
233 * Implemented for backward compatibility with Symfony < 2.5.
235 * @throws BadMethodCallException
237 * @deprecated since version 2.5, to be removed in 3.0.
239 public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath)
241 throw new BadMethodCallException('Not supported.');