Version 1
[yaffs-website] / vendor / symfony / validator / Constraints / ChoiceValidator.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\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
18 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20 /**
21  * ChoiceValidator validates that the value is one of the expected values.
22  *
23  * @author Fabien Potencier <fabien@symfony.com>
24  * @author Florian Eckerstorfer <florian@eckerstorfer.org>
25  * @author Bernhard Schussek <bschussek@gmail.com>
26  */
27 class ChoiceValidator extends ConstraintValidator
28 {
29     /**
30      * {@inheritdoc}
31      */
32     public function validate($value, Constraint $constraint)
33     {
34         if (!$constraint instanceof Choice) {
35             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
36         }
37
38         if (!is_array($constraint->choices) && !$constraint->callback) {
39             throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
40         }
41
42         if (null === $value) {
43             return;
44         }
45
46         if ($constraint->multiple && !is_array($value)) {
47             throw new UnexpectedTypeException($value, 'array');
48         }
49
50         if ($constraint->callback) {
51             if (!is_callable($choices = array($this->context->getClassName(), $constraint->callback))
52                 && !is_callable($choices = $constraint->callback)
53             ) {
54                 throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
55             }
56             $choices = call_user_func($choices);
57         } else {
58             $choices = $constraint->choices;
59         }
60
61         if ($constraint->multiple) {
62             foreach ($value as $_value) {
63                 if (!in_array($_value, $choices, $constraint->strict)) {
64                     if ($this->context instanceof ExecutionContextInterface) {
65                         $this->context->buildViolation($constraint->multipleMessage)
66                             ->setParameter('{{ value }}', $this->formatValue($_value))
67                             ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
68                             ->setInvalidValue($_value)
69                             ->addViolation();
70                     } else {
71                         $this->buildViolation($constraint->multipleMessage)
72                             ->setParameter('{{ value }}', $this->formatValue($_value))
73                             ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
74                             ->setInvalidValue($_value)
75                             ->addViolation();
76                     }
77
78                     return;
79                 }
80             }
81
82             $count = count($value);
83
84             if ($constraint->min !== null && $count < $constraint->min) {
85                 if ($this->context instanceof ExecutionContextInterface) {
86                     $this->context->buildViolation($constraint->minMessage)
87                         ->setParameter('{{ limit }}', $constraint->min)
88                         ->setPlural((int) $constraint->min)
89                         ->setCode(Choice::TOO_FEW_ERROR)
90                         ->addViolation();
91                 } else {
92                     $this->buildViolation($constraint->minMessage)
93                         ->setParameter('{{ limit }}', $constraint->min)
94                         ->setPlural((int) $constraint->min)
95                         ->setCode(Choice::TOO_FEW_ERROR)
96                         ->addViolation();
97                 }
98
99                 return;
100             }
101
102             if ($constraint->max !== null && $count > $constraint->max) {
103                 if ($this->context instanceof ExecutionContextInterface) {
104                     $this->context->buildViolation($constraint->maxMessage)
105                         ->setParameter('{{ limit }}', $constraint->max)
106                         ->setPlural((int) $constraint->max)
107                         ->setCode(Choice::TOO_MANY_ERROR)
108                         ->addViolation();
109                 } else {
110                     $this->buildViolation($constraint->maxMessage)
111                         ->setParameter('{{ limit }}', $constraint->max)
112                         ->setPlural((int) $constraint->max)
113                         ->setCode(Choice::TOO_MANY_ERROR)
114                         ->addViolation();
115                 }
116
117                 return;
118             }
119         } elseif (!in_array($value, $choices, $constraint->strict)) {
120             if ($this->context instanceof ExecutionContextInterface) {
121                 $this->context->buildViolation($constraint->message)
122                     ->setParameter('{{ value }}', $this->formatValue($value))
123                     ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
124                     ->addViolation();
125             } else {
126                 $this->buildViolation($constraint->message)
127                     ->setParameter('{{ value }}', $this->formatValue($value))
128                     ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
129                     ->addViolation();
130             }
131         }
132     }
133 }