vendor/symfony/validator/Constraints/LengthValidator.php line 34

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Validator\Exception\UnexpectedValueException;
  15. /**
  16.  * @author Bernhard Schussek <bschussek@gmail.com>
  17.  */
  18. class LengthValidator extends ConstraintValidator
  19. {
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     public function validate($valueConstraint $constraint)
  24.     {
  25.         if (!$constraint instanceof Length) {
  26.             throw new UnexpectedTypeException($constraintLength::class);
  27.         }
  28.         if (null !== $constraint->min && null === $constraint->allowEmptyString) {
  29.             @trigger_error(sprintf('Using the "%s" constraint with the "min" option without setting the "allowEmptyString" one is deprecated and defaults to true. In 5.0, it will become optional and default to false.'Length::class), E_USER_DEPRECATED);
  30.         }
  31.         if (null === $value || ('' === $value && ($constraint->allowEmptyString ?? true))) {
  32.             return;
  33.         }
  34.         if (!is_scalar($value) && !(\is_object($value) && method_exists($value'__toString'))) {
  35.             throw new UnexpectedValueException($value'string');
  36.         }
  37.         $stringValue = (string) $value;
  38.         if (null !== $constraint->normalizer) {
  39.             $stringValue = ($constraint->normalizer)($stringValue);
  40.         }
  41.         try {
  42.             $invalidCharset = !@mb_check_encoding($stringValue$constraint->charset);
  43.         } catch (\ValueError $e) {
  44.             if (!str_starts_with($e->getMessage(), 'mb_check_encoding(): Argument #2 ($encoding) must be a valid encoding')) {
  45.                 throw $e;
  46.             }
  47.             $invalidCharset true;
  48.         }
  49.         if ($invalidCharset) {
  50.             $this->context->buildViolation($constraint->charsetMessage)
  51.                 ->setParameter('{{ value }}'$this->formatValue($stringValue))
  52.                 ->setParameter('{{ charset }}'$constraint->charset)
  53.                 ->setInvalidValue($value)
  54.                 ->setCode(Length::INVALID_CHARACTERS_ERROR)
  55.                 ->addViolation();
  56.             return;
  57.         }
  58.         $length mb_strlen($stringValue$constraint->charset);
  59.         if (null !== $constraint->max && $length $constraint->max) {
  60.             $this->context->buildViolation($constraint->min == $constraint->max $constraint->exactMessage $constraint->maxMessage)
  61.                 ->setParameter('{{ value }}'$this->formatValue($stringValue))
  62.                 ->setParameter('{{ limit }}'$constraint->max)
  63.                 ->setInvalidValue($value)
  64.                 ->setPlural((int) $constraint->max)
  65.                 ->setCode(Length::TOO_LONG_ERROR)
  66.                 ->addViolation();
  67.             return;
  68.         }
  69.         if (null !== $constraint->min && $length $constraint->min) {
  70.             $this->context->buildViolation($constraint->min == $constraint->max $constraint->exactMessage $constraint->minMessage)
  71.                 ->setParameter('{{ value }}'$this->formatValue($stringValue))
  72.                 ->setParameter('{{ limit }}'$constraint->min)
  73.                 ->setInvalidValue($value)
  74.                 ->setPlural((int) $constraint->min)
  75.                 ->setCode(Length::TOO_SHORT_ERROR)
  76.                 ->addViolation();
  77.         }
  78.     }
  79. }