comparison vendor/symfony/validator/Constraints/ChoiceValidator.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\Constraints;
13
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18
19 /**
20 * ChoiceValidator validates that the value is one of the expected values.
21 *
22 * @author Fabien Potencier <fabien@symfony.com>
23 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
24 * @author Bernhard Schussek <bschussek@gmail.com>
25 */
26 class ChoiceValidator extends ConstraintValidator
27 {
28 /**
29 * {@inheritdoc}
30 */
31 public function validate($value, Constraint $constraint)
32 {
33 if (!$constraint instanceof Choice) {
34 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
35 }
36
37 if (!is_array($constraint->choices) && !$constraint->callback) {
38 throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
39 }
40
41 if (null === $value) {
42 return;
43 }
44
45 if ($constraint->multiple && !is_array($value)) {
46 throw new UnexpectedTypeException($value, 'array');
47 }
48
49 if ($constraint->callback) {
50 if (!is_callable($choices = array($this->context->getObject(), $constraint->callback))
51 && !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 (false === $constraint->strict) {
62 @trigger_error('Setting the strict option of the Choice constraint to false is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED);
63 }
64
65 if ($constraint->multiple) {
66 foreach ($value as $_value) {
67 if (!in_array($_value, $choices, $constraint->strict)) {
68 $this->context->buildViolation($constraint->multipleMessage)
69 ->setParameter('{{ value }}', $this->formatValue($_value))
70 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
71 ->setInvalidValue($_value)
72 ->addViolation();
73
74 return;
75 }
76 }
77
78 $count = count($value);
79
80 if ($constraint->min !== null && $count < $constraint->min) {
81 $this->context->buildViolation($constraint->minMessage)
82 ->setParameter('{{ limit }}', $constraint->min)
83 ->setPlural((int) $constraint->min)
84 ->setCode(Choice::TOO_FEW_ERROR)
85 ->addViolation();
86
87 return;
88 }
89
90 if ($constraint->max !== null && $count > $constraint->max) {
91 $this->context->buildViolation($constraint->maxMessage)
92 ->setParameter('{{ limit }}', $constraint->max)
93 ->setPlural((int) $constraint->max)
94 ->setCode(Choice::TOO_MANY_ERROR)
95 ->addViolation();
96
97 return;
98 }
99 } elseif (!in_array($value, $choices, $constraint->strict)) {
100 $this->context->buildViolation($constraint->message)
101 ->setParameter('{{ value }}', $this->formatValue($value))
102 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
103 ->addViolation();
104 }
105 }
106 }