cbe90e09571f8baee1529895a0bdaab22889eb76
[yaffs-website] / vendor / symfony / validator / Constraints / CountValidator.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\UnexpectedTypeException;
18
19 /**
20  * @author Bernhard Schussek <bschussek@gmail.com>
21  */
22 class CountValidator extends ConstraintValidator
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function validate($value, Constraint $constraint)
28     {
29         if (null === $value) {
30             return;
31         }
32
33         if (!is_array($value) && !$value instanceof \Countable) {
34             throw new UnexpectedTypeException($value, 'array or \Countable');
35         }
36
37         $count = count($value);
38
39         if (null !== $constraint->max && $count > $constraint->max) {
40             if ($this->context instanceof ExecutionContextInterface) {
41                 $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
42                     ->setParameter('{{ count }}', $count)
43                     ->setParameter('{{ limit }}', $constraint->max)
44                     ->setInvalidValue($value)
45                     ->setPlural((int) $constraint->max)
46                     ->setCode(Count::TOO_MANY_ERROR)
47                     ->addViolation();
48             } else {
49                 $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
50                     ->setParameter('{{ count }}', $count)
51                     ->setParameter('{{ limit }}', $constraint->max)
52                     ->setInvalidValue($value)
53                     ->setPlural((int) $constraint->max)
54                     ->setCode(Count::TOO_MANY_ERROR)
55                     ->addViolation();
56             }
57
58             return;
59         }
60
61         if (null !== $constraint->min && $count < $constraint->min) {
62             if ($this->context instanceof ExecutionContextInterface) {
63                 $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
64                     ->setParameter('{{ count }}', $count)
65                     ->setParameter('{{ limit }}', $constraint->min)
66                     ->setInvalidValue($value)
67                     ->setPlural((int) $constraint->min)
68                     ->setCode(Count::TOO_FEW_ERROR)
69                     ->addViolation();
70             } else {
71                 $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
72                     ->setParameter('{{ count }}', $count)
73                     ->setParameter('{{ limit }}', $constraint->min)
74                     ->setInvalidValue($value)
75                     ->setPlural((int) $constraint->min)
76                     ->setCode(Count::TOO_FEW_ERROR)
77                     ->addViolation();
78             }
79         }
80     }
81 }