83099aa5e3762a3cd944f4fd1c03d52baad3d8a4
[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(
126                 'The constraint "%s" can only be put on classes. Please use '.
127                 '"Symfony\Component\Validator\Constraints\Valid" instead.',
128                 get_class($constraint)
129             ));
130         }
131
132         if ($constraint instanceof Valid && null === $constraint->groups) {
133             $this->cascadingStrategy = CascadingStrategy::CASCADE;
134
135             if ($constraint->traverse) {
136                 $this->traversalStrategy = TraversalStrategy::IMPLICIT;
137             } else {
138                 $this->traversalStrategy = TraversalStrategy::NONE;
139             }
140
141             return $this;
142         }
143
144         $this->constraints[] = $constraint;
145
146         foreach ($constraint->groups as $group) {
147             $this->constraintsByGroup[$group][] = $constraint;
148         }
149
150         return $this;
151     }
152
153     /**
154      * Adds an list of constraints.
155      *
156      * @param Constraint[] $constraints The constraints to add
157      *
158      * @return $this
159      */
160     public function addConstraints(array $constraints)
161     {
162         foreach ($constraints as $constraint) {
163             $this->addConstraint($constraint);
164         }
165
166         return $this;
167     }
168
169     /**
170      * {@inheritdoc}
171      */
172     public function getConstraints()
173     {
174         return $this->constraints;
175     }
176
177     /**
178      * Returns whether this element has any constraints.
179      *
180      * @return bool
181      */
182     public function hasConstraints()
183     {
184         return count($this->constraints) > 0;
185     }
186
187     /**
188      * {@inheritdoc}
189      *
190      * Aware of the global group (* group).
191      */
192     public function findConstraints($group)
193     {
194         return isset($this->constraintsByGroup[$group])
195             ? $this->constraintsByGroup[$group]
196             : array();
197     }
198
199     /**
200      * {@inheritdoc}
201      */
202     public function getCascadingStrategy()
203     {
204         return $this->cascadingStrategy;
205     }
206
207     /**
208      * {@inheritdoc}
209      */
210     public function getTraversalStrategy()
211     {
212         return $this->traversalStrategy;
213     }
214 }