Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/validator/Constraints/TimeValidator.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Component\Validator\Constraints; | |
13 | |
14 use Symfony\Component\Validator\Constraint; | |
15 use Symfony\Component\Validator\ConstraintValidator; | |
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException; | |
17 | |
18 /** | |
19 * @author Bernhard Schussek <bschussek@gmail.com> | |
20 */ | |
21 class TimeValidator extends ConstraintValidator | |
22 { | |
23 const PATTERN = '/^(\d{2}):(\d{2}):(\d{2})$/'; | |
24 | |
25 /** | |
26 * Checks whether a time is valid. | |
27 * | |
28 * @param int $hour The hour | |
29 * @param int $minute The minute | |
30 * @param int $second The second | |
31 * | |
32 * @return bool Whether the time is valid | |
33 * | |
34 * @internal | |
35 */ | |
36 public static function checkTime($hour, $minute, $second) | |
37 { | |
38 return $hour >= 0 && $hour < 24 && $minute >= 0 && $minute < 60 && $second >= 0 && $second < 60; | |
39 } | |
40 | |
41 /** | |
42 * {@inheritdoc} | |
43 */ | |
44 public function validate($value, Constraint $constraint) | |
45 { | |
46 if (!$constraint instanceof Time) { | |
47 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Time'); | |
48 } | |
49 | |
50 if (null === $value || '' === $value || $value instanceof \DateTime) { | |
51 return; | |
52 } | |
53 | |
54 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { | |
55 throw new UnexpectedTypeException($value, 'string'); | |
56 } | |
57 | |
58 $value = (string) $value; | |
59 | |
60 if (!preg_match(static::PATTERN, $value, $matches)) { | |
61 $this->context->buildViolation($constraint->message) | |
62 ->setParameter('{{ value }}', $this->formatValue($value)) | |
63 ->setCode(Time::INVALID_FORMAT_ERROR) | |
64 ->addViolation(); | |
65 | |
66 return; | |
67 } | |
68 | |
69 if (!self::checkTime($matches[1], $matches[2], $matches[3])) { | |
70 $this->context->buildViolation($constraint->message) | |
71 ->setParameter('{{ value }}', $this->formatValue($value)) | |
72 ->setCode(Time::INVALID_TIME_ERROR) | |
73 ->addViolation(); | |
74 } | |
75 } | |
76 } |