Drupal investigation

LengthValidator.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
  36. $length = mb_strlen($stringValue, $constraint->charset);
  37. }
  38. if ($invalidCharset) {
  39. if ($this->context instanceof ExecutionContextInterface) {
  40. $this->context->buildViolation($constraint->charsetMessage)
  41. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  42. ->setParameter('{{ charset }}', $constraint->charset)
  43. ->setInvalidValue($value)
  44. ->setCode(Length::INVALID_CHARACTERS_ERROR)
  45. ->addViolation();
  46. } else {
  47. $this->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. }
  54. return;
  55. }
  56. if (null !== $constraint->max && $length > $constraint->max) {
  57. if ($this->context instanceof ExecutionContextInterface) {
  58. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
  59. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  60. ->setParameter('{{ limit }}', $constraint->max)
  61. ->setInvalidValue($value)
  62. ->setPlural((int) $constraint->max)
  63. ->setCode(Length::TOO_LONG_ERROR)
  64. ->addViolation();
  65. } else {
  66. $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
  67. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  68. ->setParameter('{{ limit }}', $constraint->max)
  69. ->setInvalidValue($value)
  70. ->setPlural((int) $constraint->max)
  71. ->setCode(Length::TOO_LONG_ERROR)
  72. ->addViolation();
  73. }
  74. return;
  75. }
  76. if (null !== $constraint->min && $length < $constraint->min) {
  77. if ($this->context instanceof ExecutionContextInterface) {
  78. $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  79. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  80. ->setParameter('{{ limit }}', $constraint->min)
  81. ->setInvalidValue($value)
  82. ->setPlural((int) $constraint->min)
  83. ->setCode(Length::TOO_SHORT_ERROR)
  84. ->addViolation();
  85. } else {
  86. $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)
  87. ->setParameter('{{ value }}', $this->formatValue($stringValue))
  88. ->setParameter('{{ limit }}', $constraint->min)
  89. ->setInvalidValue($value)
  90. ->setPlural((int) $constraint->min)
  91. ->setCode(Length::TOO_SHORT_ERROR)
  92. ->addViolation();
  93. }
  94. }
  95. }
  96. }