Drupal investigation

DateTimeValidator.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Exception\UnexpectedTypeException;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class DateTimeValidator extends DateValidator
  18. {
  19. const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/';
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function validate($value, Constraint $constraint)
  24. {
  25. if (!$constraint instanceof DateTime) {
  26. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
  27. }
  28. if (null === $value || '' === $value || $value instanceof \DateTime) {
  29. return;
  30. }
  31. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  32. throw new UnexpectedTypeException($value, 'string');
  33. }
  34. $value = (string) $value;
  35. if (!preg_match(static::PATTERN, $value, $matches)) {
  36. if ($this->context instanceof ExecutionContextInterface) {
  37. $this->context->buildViolation($constraint->message)
  38. ->setParameter('{{ value }}', $this->formatValue($value))
  39. ->setCode(DateTime::INVALID_FORMAT_ERROR)
  40. ->addViolation();
  41. } else {
  42. $this->buildViolation($constraint->message)
  43. ->setParameter('{{ value }}', $this->formatValue($value))
  44. ->setCode(DateTime::INVALID_FORMAT_ERROR)
  45. ->addViolation();
  46. }
  47. return;
  48. }
  49. if (!DateValidator::checkDate($matches[1], $matches[2], $matches[3])) {
  50. if ($this->context instanceof ExecutionContextInterface) {
  51. $this->context->buildViolation($constraint->message)
  52. ->setParameter('{{ value }}', $this->formatValue($value))
  53. ->setCode(DateTime::INVALID_DATE_ERROR)
  54. ->addViolation();
  55. } else {
  56. $this->buildViolation($constraint->message)
  57. ->setParameter('{{ value }}', $this->formatValue($value))
  58. ->setCode(DateTime::INVALID_DATE_ERROR)
  59. ->addViolation();
  60. }
  61. }
  62. if (!TimeValidator::checkTime($matches[4], $matches[5], $matches[6])) {
  63. if ($this->context instanceof ExecutionContextInterface) {
  64. $this->context->buildViolation($constraint->message)
  65. ->setParameter('{{ value }}', $this->formatValue($value))
  66. ->setCode(DateTime::INVALID_TIME_ERROR)
  67. ->addViolation();
  68. } else {
  69. $this->buildViolation($constraint->message)
  70. ->setParameter('{{ value }}', $this->formatValue($value))
  71. ->setCode(DateTime::INVALID_TIME_ERROR)
  72. ->addViolation();
  73. }
  74. }
  75. }
  76. }