1518ecf0c64e83826305c93192a4e7ae3eb52ccf
[yaffs-website] / vendor / symfony / validator / Constraints / LengthValidator.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 LengthValidator extends ConstraintValidator
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function validate($value, Constraint $constraint)
28     {
29         if (!$constraint instanceof Length) {
30             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
31         }
32
33         if (null === $value || '' === $value) {
34             return;
35         }
36
37         if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
38             throw new UnexpectedTypeException($value, 'string');
39         }
40
41         $stringValue = (string) $value;
42
43         if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
44             $length = mb_strlen($stringValue, $constraint->charset);
45         }
46
47         if ($invalidCharset) {
48             if ($this->context instanceof ExecutionContextInterface) {
49                 $this->context->buildViolation($constraint->charsetMessage)
50                     ->setParameter('{{ value }}', $this->formatValue($stringValue))
51                     ->setParameter('{{ charset }}', $constraint->charset)
52                     ->setInvalidValue($value)
53                     ->setCode(Length::INVALID_CHARACTERS_ERROR)
54                     ->addViolation();
55             } else {
56                 $this->buildViolation($constraint->charsetMessage)
57                     ->setParameter('{{ value }}', $this->formatValue($stringValue))
58                     ->setParameter('{{ charset }}', $constraint->charset)
59                     ->setInvalidValue($value)
60                     ->setCode(Length::INVALID_CHARACTERS_ERROR)
61                     ->addViolation();
62             }
63
64             return;
65         }
66
67         if (null !== $constraint->max && $length > $constraint->max) {
68             if ($this->context instanceof ExecutionContextInterface) {
69                 $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
70                     ->setParameter('{{ value }}', $this->formatValue($stringValue))
71                     ->setParameter('{{ limit }}', $constraint->max)
72                     ->setInvalidValue($value)
73                     ->setPlural((int) $constraint->max)
74                     ->setCode(Length::TOO_LONG_ERROR)
75                     ->addViolation();
76             } else {
77                 $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
78                     ->setParameter('{{ value }}', $this->formatValue($stringValue))
79                     ->setParameter('{{ limit }}', $constraint->max)
80                     ->setInvalidValue($value)
81                     ->setPlural((int) $constraint->max)
82                     ->setCode(Length::TOO_LONG_ERROR)
83                     ->addViolation();
84             }
85
86             return;
87         }
88
89         if (null !== $constraint->min && $length < $constraint->min) {
90             if ($this->context instanceof ExecutionContextInterface) {
91                 $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
92                     ->setParameter('{{ value }}', $this->formatValue($stringValue))
93                     ->setParameter('{{ limit }}', $constraint->min)
94                     ->setInvalidValue($value)
95                     ->setPlural((int) $constraint->min)
96                     ->setCode(Length::TOO_SHORT_ERROR)
97                     ->addViolation();
98             } else {
99                 $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
100                     ->setParameter('{{ value }}', $this->formatValue($stringValue))
101                     ->setParameter('{{ limit }}', $constraint->min)
102                     ->setInvalidValue($value)
103                     ->setPlural((int) $constraint->min)
104                     ->setCode(Length::TOO_SHORT_ERROR)
105                     ->addViolation();
106             }
107         }
108     }
109 }