b5b7b9ccd64649ca61848aa68cf00f54ed2e9009
[yaffs-website] / vendor / symfony / validator / Constraints / TimeValidator.php
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\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18
19 /**
20  * @author Bernhard Schussek <bschussek@gmail.com>
21  */
22 class TimeValidator extends ConstraintValidator
23 {
24     const PATTERN = '/^(\d{2}):(\d{2}):(\d{2})$/';
25
26     /**
27      * Checks whether a time is valid.
28      *
29      * @param int $hour   The hour
30      * @param int $minute The minute
31      * @param int $second The second
32      *
33      * @return bool Whether the time is valid
34      *
35      * @internal
36      */
37     public static function checkTime($hour, $minute, $second)
38     {
39         return $hour >= 0 && $hour < 24 && $minute >= 0 && $minute < 60 && $second >= 0 && $second < 60;
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     public function validate($value, Constraint $constraint)
46     {
47         if (!$constraint instanceof Time) {
48             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Time');
49         }
50
51         if (null === $value || '' === $value || $value instanceof \DateTime) {
52             return;
53         }
54
55         if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
56             throw new UnexpectedTypeException($value, 'string');
57         }
58
59         $value = (string) $value;
60
61         if (!preg_match(static::PATTERN, $value, $matches)) {
62             if ($this->context instanceof ExecutionContextInterface) {
63                 $this->context->buildViolation($constraint->message)
64                     ->setParameter('{{ value }}', $this->formatValue($value))
65                     ->setCode(Time::INVALID_FORMAT_ERROR)
66                     ->addViolation();
67             } else {
68                 $this->buildViolation($constraint->message)
69                     ->setParameter('{{ value }}', $this->formatValue($value))
70                     ->setCode(Time::INVALID_FORMAT_ERROR)
71                     ->addViolation();
72             }
73
74             return;
75         }
76
77         if (!self::checkTime($matches[1], $matches[2], $matches[3])) {
78             if ($this->context instanceof ExecutionContextInterface) {
79                 $this->context->buildViolation($constraint->message)
80                     ->setParameter('{{ value }}', $this->formatValue($value))
81                     ->setCode(Time::INVALID_TIME_ERROR)
82                     ->addViolation();
83             } else {
84                 $this->buildViolation($constraint->message)
85                     ->setParameter('{{ value }}', $this->formatValue($value))
86                     ->setCode(Time::INVALID_TIME_ERROR)
87                     ->addViolation();
88             }
89         }
90     }
91 }