721766a2533830fa104d10476f785e5d1ac17211
[yaffs-website] / vendor / symfony / validator / Context / ExecutionContext.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\Context;
13
14 use Symfony\Component\Translation\TranslatorInterface;
15 use Symfony\Component\Validator\ClassBasedInterface;
16 use Symfony\Component\Validator\Constraint;
17 use Symfony\Component\Validator\Constraints\Valid;
18 use Symfony\Component\Validator\ConstraintViolation;
19 use Symfony\Component\Validator\ConstraintViolationList;
20 use Symfony\Component\Validator\Mapping\MetadataInterface;
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\ValidatorInterface as LegacyValidatorInterface;
25 use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
26
27 /**
28  * The context used and created by {@link ExecutionContextFactory}.
29  *
30  * @author Bernhard Schussek <bschussek@gmail.com>
31  *
32  * @see ExecutionContextInterface
33  *
34  * @internal You should not instantiate or use this class. Code against
35  *           {@link ExecutionContextInterface} instead.
36  */
37 class ExecutionContext implements ExecutionContextInterface
38 {
39     /**
40      * @var ValidatorInterface
41      */
42     private $validator;
43
44     /**
45      * The root value of the validated object graph.
46      *
47      * @var mixed
48      */
49     private $root;
50
51     /**
52      * @var TranslatorInterface
53      */
54     private $translator;
55
56     /**
57      * @var string
58      */
59     private $translationDomain;
60
61     /**
62      * The violations generated in the current context.
63      *
64      * @var ConstraintViolationList
65      */
66     private $violations;
67
68     /**
69      * The currently validated value.
70      *
71      * @var mixed
72      */
73     private $value;
74
75     /**
76      * The currently validated object.
77      *
78      * @var object|null
79      */
80     private $object;
81
82     /**
83      * The property path leading to the current value.
84      *
85      * @var string
86      */
87     private $propertyPath = '';
88
89     /**
90      * The current validation metadata.
91      *
92      * @var MetadataInterface|null
93      */
94     private $metadata;
95
96     /**
97      * The currently validated group.
98      *
99      * @var string|null
100      */
101     private $group;
102
103     /**
104      * The currently validated constraint.
105      *
106      * @var Constraint|null
107      */
108     private $constraint;
109
110     /**
111      * Stores which objects have been validated in which group.
112      *
113      * @var array
114      */
115     private $validatedObjects = array();
116
117     /**
118      * Stores which class constraint has been validated for which object.
119      *
120      * @var array
121      */
122     private $validatedConstraints = array();
123
124     /**
125      * Stores which objects have been initialized.
126      *
127      * @var array
128      */
129     private $initializedObjects;
130
131     /**
132      * Creates a new execution context.
133      *
134      * @param ValidatorInterface  $validator         The validator
135      * @param mixed               $root              The root value of the
136      *                                               validated object graph
137      * @param TranslatorInterface $translator        The translator
138      * @param string|null         $translationDomain The translation domain to
139      *                                               use for translating
140      *                                               violation messages
141      *
142      * @internal Called by {@link ExecutionContextFactory}. Should not be used
143      *           in user code.
144      */
145     public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = null)
146     {
147         $this->validator = $validator;
148         $this->root = $root;
149         $this->translator = $translator;
150         $this->translationDomain = $translationDomain;
151         $this->violations = new ConstraintViolationList();
152     }
153
154     /**
155      * {@inheritdoc}
156      */
157     public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath)
158     {
159         $this->value = $value;
160         $this->object = $object;
161         $this->metadata = $metadata;
162         $this->propertyPath = (string) $propertyPath;
163     }
164
165     /**
166      * {@inheritdoc}
167      */
168     public function setGroup($group)
169     {
170         $this->group = $group;
171     }
172
173     /**
174      * {@inheritdoc}
175      */
176     public function setConstraint(Constraint $constraint)
177     {
178         $this->constraint = $constraint;
179     }
180
181     /**
182      * {@inheritdoc}
183      */
184     public function addViolation($message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
185     {
186         // The parameters $invalidValue and following are ignored by the new
187         // API, as they are not present in the new interface anymore.
188         // You should use buildViolation() instead.
189         if (func_num_args() > 2) {
190             @trigger_error('The parameters $invalidValue, $plural and $code in method '.__METHOD__.' are deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
191
192             $this
193                 ->buildViolation($message, $parameters)
194                 ->setInvalidValue($invalidValue)
195                 ->setPlural($plural)
196                 ->setCode($code)
197                 ->addViolation()
198             ;
199
200             return;
201         }
202
203         $this->violations->add(new ConstraintViolation(
204             $this->translator->trans($message, $parameters, $this->translationDomain),
205             $message,
206             $parameters,
207             $this->root,
208             $this->propertyPath,
209             $this->value,
210             null,
211             null,
212             $this->constraint
213         ));
214     }
215
216     /**
217      * {@inheritdoc}
218      */
219     public function buildViolation($message, array $parameters = array())
220     {
221         return new ConstraintViolationBuilder(
222             $this->violations,
223             $this->constraint,
224             $message,
225             $parameters,
226             $this->root,
227             $this->propertyPath,
228             $this->value,
229             $this->translator,
230             $this->translationDomain
231         );
232     }
233
234     /**
235      * {@inheritdoc}
236      */
237     public function getViolations()
238     {
239         return $this->violations;
240     }
241
242     /**
243      * {@inheritdoc}
244      */
245     public function getValidator()
246     {
247         return $this->validator;
248     }
249
250     /**
251      * {@inheritdoc}
252      */
253     public function getRoot()
254     {
255         return $this->root;
256     }
257
258     /**
259      * {@inheritdoc}
260      */
261     public function getValue()
262     {
263         return $this->value;
264     }
265
266     /**
267      * {@inheritdoc}
268      */
269     public function getObject()
270     {
271         return $this->object;
272     }
273
274     /**
275      * {@inheritdoc}
276      */
277     public function getMetadata()
278     {
279         return $this->metadata;
280     }
281
282     /**
283      * {@inheritdoc}
284      */
285     public function getGroup()
286     {
287         return $this->group;
288     }
289
290     public function getConstraint()
291     {
292         return $this->constraint;
293     }
294
295     /**
296      * {@inheritdoc}
297      */
298     public function getClassName()
299     {
300         return $this->metadata instanceof ClassBasedInterface ? $this->metadata->getClassName() : null;
301     }
302
303     /**
304      * {@inheritdoc}
305      */
306     public function getPropertyName()
307     {
308         return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata->getPropertyName() : null;
309     }
310
311     /**
312      * {@inheritdoc}
313      */
314     public function getPropertyPath($subPath = '')
315     {
316         return PropertyPath::append($this->propertyPath, $subPath);
317     }
318
319     /**
320      * {@inheritdoc}
321      */
322     public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
323     {
324         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
325
326         if (func_num_args() > 2) {
327             $this
328                 ->buildViolation($message, $parameters)
329                 ->atPath($subPath)
330                 ->setInvalidValue($invalidValue)
331                 ->setPlural($plural)
332                 ->setCode($code)
333                 ->addViolation()
334             ;
335
336             return;
337         }
338
339         $this
340             ->buildViolation($message, $parameters)
341             ->atPath($subPath)
342             ->addViolation()
343         ;
344     }
345
346     /**
347      * {@inheritdoc}
348      */
349     public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
350     {
351         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);
352
353         if (is_array($value)) {
354             // The $traverse flag is ignored for arrays
355             $constraint = new Valid(array('traverse' => true, 'deep' => $deep));
356
357             return $this
358                 ->getValidator()
359                 ->inContext($this)
360                 ->atPath($subPath)
361                 ->validate($value, $constraint, $groups)
362             ;
363         }
364
365         if ($traverse && $value instanceof \Traversable) {
366             $constraint = new Valid(array('traverse' => true, 'deep' => $deep));
367
368             return $this
369                 ->getValidator()
370                 ->inContext($this)
371                 ->atPath($subPath)
372                 ->validate($value, $constraint, $groups)
373             ;
374         }
375
376         return $this
377             ->getValidator()
378             ->inContext($this)
379             ->atPath($subPath)
380             ->validate($value, null, $groups)
381         ;
382     }
383
384     /**
385      * {@inheritdoc}
386      */
387     public function validateValue($value, $constraints, $subPath = '', $groups = null)
388     {
389         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);
390
391         return $this
392             ->getValidator()
393             ->inContext($this)
394             ->atPath($subPath)
395             ->validate($value, $constraints, $groups)
396         ;
397     }
398
399     /**
400      * {@inheritdoc}
401      */
402     public function getMetadataFactory()
403     {
404         @trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0. Use the new Symfony\Component\Validator\Context\ExecutionContext::getValidator method in combination with Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor or Symfony\Component\Validator\Validator\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);
405
406         $validator = $this->getValidator();
407
408         if ($validator instanceof LegacyValidatorInterface) {
409             return $validator->getMetadataFactory();
410         }
411
412         // The ValidatorInterface extends from the deprecated MetadataFactoryInterface, so return it when we don't have the factory instance itself
413         return $validator;
414     }
415
416     /**
417      * {@inheritdoc}
418      */
419     public function markGroupAsValidated($cacheKey, $groupHash)
420     {
421         if (!isset($this->validatedObjects[$cacheKey])) {
422             $this->validatedObjects[$cacheKey] = array();
423         }
424
425         $this->validatedObjects[$cacheKey][$groupHash] = true;
426     }
427
428     /**
429      * {@inheritdoc}
430      */
431     public function isGroupValidated($cacheKey, $groupHash)
432     {
433         return isset($this->validatedObjects[$cacheKey][$groupHash]);
434     }
435
436     /**
437      * {@inheritdoc}
438      */
439     public function markConstraintAsValidated($cacheKey, $constraintHash)
440     {
441         $this->validatedConstraints[$cacheKey.':'.$constraintHash] = true;
442     }
443
444     /**
445      * {@inheritdoc}
446      */
447     public function isConstraintValidated($cacheKey, $constraintHash)
448     {
449         return isset($this->validatedConstraints[$cacheKey.':'.$constraintHash]);
450     }
451
452     /**
453      * {@inheritdoc}
454      */
455     public function markObjectAsInitialized($cacheKey)
456     {
457         $this->initializedObjects[$cacheKey] = true;
458     }
459
460     /**
461      * {@inheritdoc}
462      */
463     public function isObjectInitialized($cacheKey)
464     {
465         return isset($this->initializedObjects[$cacheKey]);
466     }
467 }