4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
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;
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class LengthValidator extends ConstraintValidator
27 public function validate($value, Constraint $constraint)
29 if (!$constraint instanceof Length) {
30 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
33 if (null === $value || '' === $value) {
37 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
38 throw new UnexpectedTypeException($value, 'string');
41 $stringValue = (string) $value;
43 if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
44 $length = mb_strlen($stringValue, $constraint->charset);
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)
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)
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)
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)
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)
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)