Drupal investigation

RangeValidator.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 RangeValidator extends ConstraintValidator
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function validate($value, Constraint $constraint)
  24. {
  25. if (!$constraint instanceof Range) {
  26. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Range');
  27. }
  28. if (null === $value) {
  29. return;
  30. }
  31. if (!is_numeric($value) && !$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
  32. if ($this->context instanceof ExecutionContextInterface) {
  33. $this->context->buildViolation($constraint->invalidMessage)
  34. ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
  35. ->setCode(Range::INVALID_CHARACTERS_ERROR)
  36. ->addViolation();
  37. } else {
  38. $this->buildViolation($constraint->invalidMessage)
  39. ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
  40. ->setCode(Range::INVALID_CHARACTERS_ERROR)
  41. ->addViolation();
  42. }
  43. return;
  44. }
  45. $min = $constraint->min;
  46. $max = $constraint->max;
  47. // Convert strings to DateTimes if comparing another DateTime
  48. // This allows to compare with any date/time value supported by
  49. // the DateTime constructor:
  50. // http://php.net/manual/en/datetime.formats.php
  51. if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
  52. if (is_string($min)) {
  53. $min = new \DateTime($min);
  54. }
  55. if (is_string($max)) {
  56. $max = new \DateTime($max);
  57. }
  58. }
  59. if (null !== $constraint->max && $value > $max) {
  60. if ($this->context instanceof ExecutionContextInterface) {
  61. $this->context->buildViolation($constraint->maxMessage)
  62. ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
  63. ->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
  64. ->setCode(Range::TOO_HIGH_ERROR)
  65. ->addViolation();
  66. } else {
  67. $this->buildViolation($constraint->maxMessage)
  68. ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
  69. ->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
  70. ->setCode(Range::TOO_HIGH_ERROR)
  71. ->addViolation();
  72. }
  73. return;
  74. }
  75. if (null !== $constraint->min && $value < $min) {
  76. if ($this->context instanceof ExecutionContextInterface) {
  77. $this->context->buildViolation($constraint->minMessage)
  78. ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
  79. ->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
  80. ->setCode(Range::TOO_LOW_ERROR)
  81. ->addViolation();
  82. } else {
  83. $this->buildViolation($constraint->minMessage)
  84. ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
  85. ->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
  86. ->setCode(Range::TOO_LOW_ERROR)
  87. ->addViolation();
  88. }
  89. }
  90. }
  91. }