comparison vendor/symfony/validator/Context/ExecutionContext.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\Context;
13
14 use Symfony\Component\Translation\TranslatorInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintViolation;
17 use Symfony\Component\Validator\ConstraintViolationList;
18 use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
19 use Symfony\Component\Validator\Mapping\MetadataInterface;
20 use Symfony\Component\Validator\Mapping\MemberMetadata;
21 use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
22 use Symfony\Component\Validator\Util\PropertyPath;
23 use Symfony\Component\Validator\Validator\ValidatorInterface;
24 use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
25
26 /**
27 * The context used and created by {@link ExecutionContextFactory}.
28 *
29 * @author Bernhard Schussek <bschussek@gmail.com>
30 *
31 * @see ExecutionContextInterface
32 *
33 * @internal You should not instantiate or use this class. Code against
34 * {@link ExecutionContextInterface} instead.
35 */
36 class ExecutionContext implements ExecutionContextInterface
37 {
38 /**
39 * @var ValidatorInterface
40 */
41 private $validator;
42
43 /**
44 * The root value of the validated object graph.
45 *
46 * @var mixed
47 */
48 private $root;
49
50 /**
51 * @var TranslatorInterface
52 */
53 private $translator;
54
55 /**
56 * @var string
57 */
58 private $translationDomain;
59
60 /**
61 * The violations generated in the current context.
62 *
63 * @var ConstraintViolationList
64 */
65 private $violations;
66
67 /**
68 * The currently validated value.
69 *
70 * @var mixed
71 */
72 private $value;
73
74 /**
75 * The currently validated object.
76 *
77 * @var object|null
78 */
79 private $object;
80
81 /**
82 * The property path leading to the current value.
83 *
84 * @var string
85 */
86 private $propertyPath = '';
87
88 /**
89 * The current validation metadata.
90 *
91 * @var MetadataInterface|null
92 */
93 private $metadata;
94
95 /**
96 * The currently validated group.
97 *
98 * @var string|null
99 */
100 private $group;
101
102 /**
103 * The currently validated constraint.
104 *
105 * @var Constraint|null
106 */
107 private $constraint;
108
109 /**
110 * Stores which objects have been validated in which group.
111 *
112 * @var array
113 */
114 private $validatedObjects = array();
115
116 /**
117 * Stores which class constraint has been validated for which object.
118 *
119 * @var array
120 */
121 private $validatedConstraints = array();
122
123 /**
124 * Stores which objects have been initialized.
125 *
126 * @var array
127 */
128 private $initializedObjects;
129
130 /**
131 * Creates a new execution context.
132 *
133 * @param ValidatorInterface $validator The validator
134 * @param mixed $root The root value of the
135 * validated object graph
136 * @param TranslatorInterface $translator The translator
137 * @param string|null $translationDomain The translation domain to
138 * use for translating
139 * violation messages
140 *
141 * @internal Called by {@link ExecutionContextFactory}. Should not be used
142 * in user code.
143 */
144 public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = null)
145 {
146 $this->validator = $validator;
147 $this->root = $root;
148 $this->translator = $translator;
149 $this->translationDomain = $translationDomain;
150 $this->violations = new ConstraintViolationList();
151 }
152
153 /**
154 * {@inheritdoc}
155 */
156 public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath)
157 {
158 $this->value = $value;
159 $this->object = $object;
160 $this->metadata = $metadata;
161 $this->propertyPath = (string) $propertyPath;
162 }
163
164 /**
165 * {@inheritdoc}
166 */
167 public function setGroup($group)
168 {
169 $this->group = $group;
170 }
171
172 /**
173 * {@inheritdoc}
174 */
175 public function setConstraint(Constraint $constraint)
176 {
177 $this->constraint = $constraint;
178 }
179
180 /**
181 * {@inheritdoc}
182 */
183 public function addViolation($message, array $parameters = array())
184 {
185 $this->violations->add(new ConstraintViolation(
186 $this->translator->trans($message, $parameters, $this->translationDomain),
187 $message,
188 $parameters,
189 $this->root,
190 $this->propertyPath,
191 $this->value,
192 null,
193 null,
194 $this->constraint
195 ));
196 }
197
198 /**
199 * {@inheritdoc}
200 */
201 public function buildViolation($message, array $parameters = array())
202 {
203 return new ConstraintViolationBuilder(
204 $this->violations,
205 $this->constraint,
206 $message,
207 $parameters,
208 $this->root,
209 $this->propertyPath,
210 $this->value,
211 $this->translator,
212 $this->translationDomain
213 );
214 }
215
216 /**
217 * {@inheritdoc}
218 */
219 public function getViolations()
220 {
221 return $this->violations;
222 }
223
224 /**
225 * {@inheritdoc}
226 */
227 public function getValidator()
228 {
229 return $this->validator;
230 }
231
232 /**
233 * {@inheritdoc}
234 */
235 public function getRoot()
236 {
237 return $this->root;
238 }
239
240 /**
241 * {@inheritdoc}
242 */
243 public function getValue()
244 {
245 return $this->value;
246 }
247
248 /**
249 * {@inheritdoc}
250 */
251 public function getObject()
252 {
253 return $this->object;
254 }
255
256 /**
257 * {@inheritdoc}
258 */
259 public function getMetadata()
260 {
261 return $this->metadata;
262 }
263
264 /**
265 * {@inheritdoc}
266 */
267 public function getGroup()
268 {
269 return $this->group;
270 }
271
272 public function getConstraint()
273 {
274 return $this->constraint;
275 }
276
277 /**
278 * {@inheritdoc}
279 */
280 public function getClassName()
281 {
282 return $this->metadata instanceof MemberMetadata || $this->metadata instanceof ClassMetadataInterface ? $this->metadata->getClassName() : null;
283 }
284
285 /**
286 * {@inheritdoc}
287 */
288 public function getPropertyName()
289 {
290 return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata->getPropertyName() : null;
291 }
292
293 /**
294 * {@inheritdoc}
295 */
296 public function getPropertyPath($subPath = '')
297 {
298 return PropertyPath::append($this->propertyPath, $subPath);
299 }
300
301 /**
302 * {@inheritdoc}
303 */
304 public function markGroupAsValidated($cacheKey, $groupHash)
305 {
306 if (!isset($this->validatedObjects[$cacheKey])) {
307 $this->validatedObjects[$cacheKey] = array();
308 }
309
310 $this->validatedObjects[$cacheKey][$groupHash] = true;
311 }
312
313 /**
314 * {@inheritdoc}
315 */
316 public function isGroupValidated($cacheKey, $groupHash)
317 {
318 return isset($this->validatedObjects[$cacheKey][$groupHash]);
319 }
320
321 /**
322 * {@inheritdoc}
323 */
324 public function markConstraintAsValidated($cacheKey, $constraintHash)
325 {
326 $this->validatedConstraints[$cacheKey.':'.$constraintHash] = true;
327 }
328
329 /**
330 * {@inheritdoc}
331 */
332 public function isConstraintValidated($cacheKey, $constraintHash)
333 {
334 return isset($this->validatedConstraints[$cacheKey.':'.$constraintHash]);
335 }
336
337 /**
338 * {@inheritdoc}
339 */
340 public function markObjectAsInitialized($cacheKey)
341 {
342 $this->initializedObjects[$cacheKey] = true;
343 }
344
345 /**
346 * {@inheritdoc}
347 */
348 public function isObjectInitialized($cacheKey)
349 {
350 return isset($this->initializedObjects[$cacheKey]);
351 }
352 }