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