LengthValidator.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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($value, Constraint $constraint)
  24. {
  25. if (!$constraint instanceof Length) {
  26. throw new UnexpectedTypeException($constraint, Length::class);
  27. }
  28. if (null === $value || ('' === $value && $constraint->allowEmptyString)) {
  29. return;
  30. }
  31. if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
  32. throw new UnexpectedValueException($value, 'string');
  33. }
  34. $stringValue = (string) $value;
  35. if (null !== $constraint->normalizer) {
  36. $stringValue = ($constraint->normalizer)($stringValue);
  37. }
  38. try {
  39. $invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset);
  40. } catch (\ValueError $e) {
  41. if (!str_starts_with($e->getMessage(), 'mb_check_encoding(): Argument #2 ($encoding) must be a valid encoding')) {
  42. throw $e;
  43. }
  44. $invalidCharset = true;
  45. }
  46. if ($invalidCharset) {
  47. $this->context->buildViolation($constraint->charsetMessage)
  48. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  49. ->setParameter('{{ charset }}', $constraint->charset)
  50. ->setInvalidValue($value)
  51. ->setCode(Length::INVALID_CHARACTERS_ERROR)
  52. ->addViolation();
  53. return;
  54. }
  55. $length = mb_strlen($stringValue, $constraint->charset);
  56. if (null !== $constraint->max && $length > $constraint->max) {
  57. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
  58. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  59. ->setParameter('{{ limit }}', $constraint->max)
  60. ->setInvalidValue($value)
  61. ->setPlural((int) $constraint->max)
  62. ->setCode(Length::TOO_LONG_ERROR)
  63. ->addViolation();
  64. return;
  65. }
  66. if (null !== $constraint->min && $length < $constraint->min) {
  67. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  68. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  69. ->setParameter('{{ limit }}', $constraint->min)
  70. ->setInvalidValue($value)
  71. ->setPlural((int) $constraint->min)
  72. ->setCode(Length::TOO_SHORT_ERROR)
  73. ->addViolation();
  74. }
  75. }
  76. }