af956ee06b583fae9e8f466d9ece8881768babe4
[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\Constraint;
15 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
16
17 /**
18  * @author Bernhard Schussek <bschussek@gmail.com>
19  * @author Diego Saint Esteben <diego@saintesteben.me>
20  */
21 class DateTimeValidator extends DateValidator
22 {
23     /**
24      * @deprecated since version 3.1, to be removed in 4.0.
25      */
26     const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/';
27
28     /**
29      * {@inheritdoc}
30      */
31     public function validate($value, Constraint $constraint)
32     {
33         if (!$constraint instanceof DateTime) {
34             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
35         }
36
37         if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
38             return;
39         }
40
41         if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
42             throw new UnexpectedTypeException($value, 'string');
43         }
44
45         $value = (string) $value;
46
47         \DateTime::createFromFormat($constraint->format, $value);
48
49         $errors = \DateTime::getLastErrors();
50
51         if (0 < $errors['error_count']) {
52             $this->context->buildViolation($constraint->message)
53                 ->setParameter('{{ value }}', $this->formatValue($value))
54                 ->setCode(DateTime::INVALID_FORMAT_ERROR)
55                 ->addViolation();
56
57             return;
58         }
59
60         foreach ($errors['warnings'] as $warning) {
61             if ('The parsed date was invalid' === $warning) {
62                 $this->context->buildViolation($constraint->message)
63                     ->setParameter('{{ value }}', $this->formatValue($value))
64                     ->setCode(DateTime::INVALID_DATE_ERROR)
65                     ->addViolation();
66             } elseif ('The parsed time was invalid' === $warning) {
67                 $this->context->buildViolation($constraint->message)
68                     ->setParameter('{{ value }}', $this->formatValue($value))
69                     ->setCode(DateTime::INVALID_TIME_ERROR)
70                     ->addViolation();
71             } else {
72                 $this->context->buildViolation($constraint->message)
73                     ->setParameter('{{ value }}', $this->formatValue($value))
74                     ->setCode(DateTime::INVALID_FORMAT_ERROR)
75                     ->addViolation();
76             }
77         }
78     }
79 }