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
117 * @param Constraint $constraint The constraint to add
121 * @throws ConstraintDefinitionException When trying to add the
122 * {@link Traverse} constraint
124 public function addConstraint(Constraint $constraint)
126 if ($constraint instanceof Traverse) {
127 throw new ConstraintDefinitionException(sprintf(
128 'The constraint "%s" can only be put on classes. Please use '.
129 '"Symfony\Component\Validator\Constraints\Valid" instead.',
130 get_class($constraint)
134 if ($constraint instanceof Valid) {
135 $this->cascadingStrategy = CascadingStrategy::CASCADE;
137 if ($constraint->traverse) {
138 $this->traversalStrategy = TraversalStrategy::IMPLICIT;
140 $this->traversalStrategy = TraversalStrategy::NONE;
146 $this->constraints[] = $constraint;
148 foreach ($constraint->groups as $group) {
149 $this->constraintsByGroup[$group][] = $constraint;
156 * Adds an list of constraints.
158 * @param Constraint[] $constraints The constraints to add
162 public function addConstraints(array $constraints)
164 foreach ($constraints as $constraint) {
165 $this->addConstraint($constraint);
174 public function getConstraints()
176 return $this->constraints;
180 * Returns whether this element has any constraints.
184 public function hasConstraints()
186 return count($this->constraints) > 0;
192 * Aware of the global group (* group).
194 public function findConstraints($group)
196 return isset($this->constraintsByGroup[$group])
197 ? $this->constraintsByGroup[$group]
204 public function getCascadingStrategy()
206 return $this->cascadingStrategy;
212 public function getTraversalStrategy()
214 return $this->traversalStrategy;