Security update for Core, with self-updated composer
[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      * @param Constraint $constraint The constraint to add
118      *
119      * @return $this
120      *
121      * @throws ConstraintDefinitionException When trying to add the
122      *                                       {@link Traverse} constraint
123      */
124     public function addConstraint(Constraint $constraint)
125     {
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)
131             ));
132         }
133
134         if ($constraint instanceof Valid) {
135             $this->cascadingStrategy = CascadingStrategy::CASCADE;
136
137             if ($constraint->traverse) {
138                 $this->traversalStrategy = TraversalStrategy::IMPLICIT;
139             } else {
140                 $this->traversalStrategy = TraversalStrategy::NONE;
141             }
142
143             return $this;
144         }
145
146         $this->constraints[] = $constraint;
147
148         foreach ($constraint->groups as $group) {
149             $this->constraintsByGroup[$group][] = $constraint;
150         }
151
152         return $this;
153     }
154
155     /**
156      * Adds an list of constraints.
157      *
158      * @param Constraint[] $constraints The constraints to add
159      *
160      * @return $this
161      */
162     public function addConstraints(array $constraints)
163     {
164         foreach ($constraints as $constraint) {
165             $this->addConstraint($constraint);
166         }
167
168         return $this;
169     }
170
171     /**
172      * {@inheritdoc}
173      */
174     public function getConstraints()
175     {
176         return $this->constraints;
177     }
178
179     /**
180      * Returns whether this element has any constraints.
181      *
182      * @return bool
183      */
184     public function hasConstraints()
185     {
186         return count($this->constraints) > 0;
187     }
188
189     /**
190      * {@inheritdoc}
191      *
192      * Aware of the global group (* group).
193      */
194     public function findConstraints($group)
195     {
196         return isset($this->constraintsByGroup[$group])
197             ? $this->constraintsByGroup[$group]
198             : array();
199     }
200
201     /**
202      * {@inheritdoc}
203      */
204     public function getCascadingStrategy()
205     {
206         return $this->cascadingStrategy;
207     }
208
209     /**
210      * {@inheritdoc}
211      */
212     public function getTraversalStrategy()
213     {
214         return $this->traversalStrategy;
215     }
216 }