Version 1
[yaffs-website] / vendor / symfony / validator / Constraints / AllValidator.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\Constraints;
13
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Context\ExecutionContextInterface;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18
19 /**
20  * @author Bernhard Schussek <bschussek@gmail.com>
21  */
22 class AllValidator extends ConstraintValidator
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function validate($value, Constraint $constraint)
28     {
29         if (!$constraint instanceof All) {
30             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\All');
31         }
32
33         if (null === $value) {
34             return;
35         }
36
37         if (!is_array($value) && !$value instanceof \Traversable) {
38             throw new UnexpectedTypeException($value, 'array or Traversable');
39         }
40
41         $context = $this->context;
42
43         if ($context instanceof ExecutionContextInterface) {
44             $validator = $context->getValidator()->inContext($context);
45
46             foreach ($value as $key => $element) {
47                 $validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
48             }
49         } else {
50             // 2.4 API
51             foreach ($value as $key => $element) {
52                 $context->validateValue($element, $constraint->constraints, '['.$key.']');
53             }
54         }
55     }
56 }