Version 1
[yaffs-website] / vendor / symfony / validator / Constraints / DateValidator.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 DateValidator extends ConstraintValidator
23 {
24     const PATTERN = '/^(\d{4})-(\d{2})-(\d{2})$/';
25
26     /**
27      * Checks whether a date is valid.
28      *
29      * @param int $year  The year
30      * @param int $month The month
31      * @param int $day   The day
32      *
33      * @return bool Whether the date is valid
34      *
35      * @internal
36      */
37     public static function checkDate($year, $month, $day)
38     {
39         return checkdate($month, $day, $year);
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     public function validate($value, Constraint $constraint)
46     {
47         if (!$constraint instanceof Date) {
48             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
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(Date::INVALID_FORMAT_ERROR)
66                     ->addViolation();
67             } else {
68                 $this->buildViolation($constraint->message)
69                     ->setParameter('{{ value }}', $this->formatValue($value))
70                     ->setCode(Date::INVALID_FORMAT_ERROR)
71                     ->addViolation();
72             }
73
74             return;
75         }
76
77         if (!self::checkDate($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(Date::INVALID_DATE_ERROR)
82                     ->addViolation();
83             } else {
84                 $this->buildViolation($constraint->message)
85                     ->setParameter('{{ value }}', $this->formatValue($value))
86                     ->setCode(Date::INVALID_DATE_ERROR)
87                     ->addViolation();
88             }
89         }
90     }
91 }