PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/TimeValidator.php

https://bitbucket.org/ingsol/music_sonata
PHP | 59 lines | 27 code | 11 blank | 21 comment | 7 complexity | 158080ba0ffb99fd20d2831b90e9bc0b MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, Apache-2.0, JSON, LGPL-3.0, BSD-3-Clause
  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. /**
  15. * @api
  16. */
  17. class TimeValidator extends ConstraintValidator
  18. {
  19. const PATTERN = '/^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/';
  20. /**
  21. * Checks if the passed value is valid.
  22. *
  23. * @param mixed $value The value that should be validated
  24. * @param Constraint $constraint The constraint for the validation
  25. *
  26. * @return Boolean Whether or not the value is valid
  27. *
  28. * @api
  29. */
  30. public function isValid($value, Constraint $constraint)
  31. {
  32. if (null === $value || '' === $value) {
  33. return true;
  34. }
  35. if ($value instanceof \DateTime) {
  36. return true;
  37. }
  38. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  39. throw new UnexpectedTypeException($value, 'string');
  40. }
  41. $value = (string) $value;
  42. if (!preg_match(static::PATTERN, $value)) {
  43. $this->setMessage($constraint->message, array('{{ value }}' => $value));
  44. return false;
  45. }
  46. return true;
  47. }
  48. }