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