comparison vendor/symfony/validator/Constraints/CountValidator.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
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\UnexpectedTypeException;
17
18 /**
19 * @author Bernhard Schussek <bschussek@gmail.com>
20 */
21 class CountValidator extends ConstraintValidator
22 {
23 /**
24 * {@inheritdoc}
25 */
26 public function validate($value, Constraint $constraint)
27 {
28 if (null === $value) {
29 return;
30 }
31
32 if (!is_array($value) && !$value instanceof \Countable) {
33 throw new UnexpectedTypeException($value, 'array or \Countable');
34 }
35
36 $count = count($value);
37
38 if (null !== $constraint->max && $count > $constraint->max) {
39 $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
40 ->setParameter('{{ count }}', $count)
41 ->setParameter('{{ limit }}', $constraint->max)
42 ->setInvalidValue($value)
43 ->setPlural((int) $constraint->max)
44 ->setCode(Count::TOO_MANY_ERROR)
45 ->addViolation();
46
47 return;
48 }
49
50 if (null !== $constraint->min && $count < $constraint->min) {
51 $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
52 ->setParameter('{{ count }}', $count)
53 ->setParameter('{{ limit }}', $constraint->min)
54 ->setInvalidValue($value)
55 ->setPlural((int) $constraint->min)
56 ->setCode(Count::TOO_FEW_ERROR)
57 ->addViolation();
58 }
59 }
60 }