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