a5364f9b08720bd117a647d72871a8259bb09f44
[yaffs-website] / vendor / symfony / validator / Mapping / GenericMetadata.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Mapping;
13
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;
18
19 /**
20  * A generic container of {@link Constraint} objects.
21  *
22  * This class supports serialization and cloning.
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     /**
38      * @var array
39      *
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.
43      */
44     public $constraintsByGroup = array();
45
46     /**
47      * The strategy for cascading objects.
48      *
49      * By default, objects are not cascaded.
50      *
51      * @var int
52      *
53      * @see CascadingStrategy
54      *
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.
58      */
59     public $cascadingStrategy = CascadingStrategy::NONE;
60
61     /**
62      * The strategy for traversing traversable objects.
63      *
64      * By default, traversable objects are not traversed.
65      *
66      * @var int
67      *
68      * @see TraversalStrategy
69      *
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.
73      */
74     public $traversalStrategy = TraversalStrategy::NONE;
75
76     /**
77      * Returns the names of the properties that should be serialized.
78      *
79      * @return string[]
80      */
81     public function __sleep()
82     {
83         return array(
84             'constraints',
85             'constraintsByGroup',
86             'cascadingStrategy',
87             'traversalStrategy',
88         );
89     }
90
91     /**
92      * Clones this object.
93      */
94     public function __clone()
95     {
96         $constraints = $this->constraints;
97
98         $this->constraints = array();
99         $this->constraintsByGroup = array();
100
101         foreach ($constraints as $constraint) {
102             $this->addConstraint(clone $constraint);
103         }
104     }
105
106     /**
107      * Adds a constraint.
108      *
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:
113      *
114      *  - {@link TraversalStrategy::IMPLICIT} if $traverse is enabled
115      *  - {@link TraversalStrategy::NONE} if $traverse is disabled
116      *
117      * @return $this
118      *
119      * @throws ConstraintDefinitionException When trying to add the
120      *                                       {@link Traverse} constraint
121      */
122     public function addConstraint(Constraint $constraint)
123     {
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)));
126         }
127
128         if ($constraint instanceof Valid && null === $constraint->groups) {
129             $this->cascadingStrategy = CascadingStrategy::CASCADE;
130
131             if ($constraint->traverse) {
132                 $this->traversalStrategy = TraversalStrategy::IMPLICIT;
133             } else {
134                 $this->traversalStrategy = TraversalStrategy::NONE;
135             }
136
137             return $this;
138         }
139
140         $this->constraints[] = $constraint;
141
142         foreach ($constraint->groups as $group) {
143             $this->constraintsByGroup[$group][] = $constraint;
144         }
145
146         return $this;
147     }
148
149     /**
150      * Adds an list of constraints.
151      *
152      * @param Constraint[] $constraints The constraints to add
153      *
154      * @return $this
155      */
156     public function addConstraints(array $constraints)
157     {
158         foreach ($constraints as $constraint) {
159             $this->addConstraint($constraint);
160         }
161
162         return $this;
163     }
164
165     /**
166      * {@inheritdoc}
167      */
168     public function getConstraints()
169     {
170         return $this->constraints;
171     }
172
173     /**
174      * Returns whether this element has any constraints.
175      *
176      * @return bool
177      */
178     public function hasConstraints()
179     {
180         return \count($this->constraints) > 0;
181     }
182
183     /**
184      * {@inheritdoc}
185      *
186      * Aware of the global group (* group).
187      */
188     public function findConstraints($group)
189     {
190         return isset($this->constraintsByGroup[$group])
191             ? $this->constraintsByGroup[$group]
192             : array();
193     }
194
195     /**
196      * {@inheritdoc}
197      */
198     public function getCascadingStrategy()
199     {
200         return $this->cascadingStrategy;
201     }
202
203     /**
204      * {@inheritdoc}
205      */
206     public function getTraversalStrategy()
207     {
208         return $this->traversalStrategy;
209     }
210 }