LengthValidator.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Context\ExecutionContextInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\ConstraintValidator;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  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, __NAMESPACE__.'\Length');
  27. }
  28. if (null === $value || '' === $value) {
  29. return;
  30. }
  31. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  32. throw new UnexpectedTypeException($value, 'string');
  33. }
  34. $stringValue = (string) $value;
  35. if ('UTF8' === $charset = strtoupper($constraint->charset)) {
  36. $charset = 'UTF-8'; // iconv on Windows requires "UTF-8" instead of "UTF8"
  37. }
  38. $length = @iconv_strlen($stringValue, $charset);
  39. $invalidCharset = false === $length;
  40. if ($invalidCharset) {
  41. if ($this->context instanceof ExecutionContextInterface) {
  42. $this->context->buildViolation($constraint->charsetMessage)
  43. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  44. ->setParameter('{{ charset }}', $constraint->charset)
  45. ->setInvalidValue($value)
  46. ->setCode(Length::INVALID_CHARACTERS_ERROR)
  47. ->addViolation();
  48. } else {
  49. $this->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. }
  56. return;
  57. }
  58. if (null !== $constraint->max && $length > $constraint->max) {
  59. if ($this->context instanceof ExecutionContextInterface) {
  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. } else {
  68. $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
  69. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  70. ->setParameter('{{ limit }}', $constraint->max)
  71. ->setInvalidValue($value)
  72. ->setPlural((int) $constraint->max)
  73. ->setCode(Length::TOO_LONG_ERROR)
  74. ->addViolation();
  75. }
  76. return;
  77. }
  78. if (null !== $constraint->min && $length < $constraint->min) {
  79. if ($this->context instanceof ExecutionContextInterface) {
  80. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  81. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  82. ->setParameter('{{ limit }}', $constraint->min)
  83. ->setInvalidValue($value)
  84. ->setPlural((int) $constraint->min)
  85. ->setCode(Length::TOO_SHORT_ERROR)
  86. ->addViolation();
  87. } else {
  88. $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  89. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  90. ->setParameter('{{ limit }}', $constraint->min)
  91. ->setInvalidValue($value)
  92. ->setPlural((int) $constraint->min)
  93. ->setCode(Length::TOO_SHORT_ERROR)
  94. ->addViolation();
  95. }
  96. }
  97. }
  98. }