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\ConstraintDefinitionException;
20 * A generic container of {@link Constraint} objects.
22 * This class supports serialization and cloning.
24 * @author Bernhard Schussek <bschussek@gmail.com>
26 class GenericMetadata implements MetadataInterface
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.
35 public $constraints = array();
40 * @internal This property is public in order to reduce the size of the
41 * class' serialized representation. Do not access it. Use
42 * {@link findConstraints()} instead.
44 public $constraintsByGroup = array();
47 * The strategy for cascading objects.
49 * By default, objects are not cascaded.
53 * @see CascadingStrategy
55 * @internal This property is public in order to reduce the size of the
56 * class' serialized representation. Do not access it. Use
57 * {@link getCascadingStrategy()} instead.
59 public $cascadingStrategy = CascadingStrategy::NONE;
62 * The strategy for traversing traversable objects.
64 * By default, traversable objects are not traversed.
68 * @see TraversalStrategy
70 * @internal This property is public in order to reduce the size of the
71 * class' serialized representation. Do not access it. Use
72 * {@link getTraversalStrategy()} instead.
74 public $traversalStrategy = TraversalStrategy::NONE;
77 * Returns the names of the properties that should be serialized.
81 public function __sleep()
94 public function __clone()
96 $constraints = $this->constraints;
98 $this->constraints = array();
99 $this->constraintsByGroup = array();
101 foreach ($constraints as $constraint) {
102 $this->addConstraint(clone $constraint);
109 * If the constraint {@link Valid} is added, the cascading strategy will be
110 * changed to {@link CascadingStrategy::CASCADE}. Depending on the
111 * $traverse property of that constraint, the traversal strategy
112 * will be set to one of the following:
114 * - {@link TraversalStrategy::IMPLICIT} if $traverse is enabled
115 * - {@link TraversalStrategy::NONE} if $traverse is disabled
119 * @throws ConstraintDefinitionException When trying to add the
120 * {@link Traverse} constraint
122 public function addConstraint(Constraint $constraint)
124 if ($constraint instanceof Traverse) {
125 throw new ConstraintDefinitionException(sprintf('The constraint "%s" can only be put on classes. Please use "Symfony\Component\Validator\Constraints\Valid" instead.', \get_class($constraint)));
128 if ($constraint instanceof Valid && null === $constraint->groups) {
129 $this->cascadingStrategy = CascadingStrategy::CASCADE;
131 if ($constraint->traverse) {
132 $this->traversalStrategy = TraversalStrategy::IMPLICIT;
134 $this->traversalStrategy = TraversalStrategy::NONE;
140 $this->constraints[] = $constraint;
142 foreach ($constraint->groups as $group) {
143 $this->constraintsByGroup[$group][] = $constraint;
150 * Adds an list of constraints.
152 * @param Constraint[] $constraints The constraints to add
156 public function addConstraints(array $constraints)
158 foreach ($constraints as $constraint) {
159 $this->addConstraint($constraint);
168 public function getConstraints()
170 return $this->constraints;
174 * Returns whether this element has any constraints.
178 public function hasConstraints()
180 return \count($this->constraints) > 0;
186 * Aware of the global group (* group).
188 public function findConstraints($group)
190 return isset($this->constraintsByGroup[$group])
191 ? $this->constraintsByGroup[$group]
198 public function getCascadingStrategy()
200 return $this->cascadingStrategy;
206 public function getTraversalStrategy()
208 return $this->traversalStrategy;