05ef3b47c752bb177b54a0f2b134e2de6c7fa1fa
[yaffs-website] / vendor / symfony / validator / Constraints / RangeValidator.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 RangeValidator extends ConstraintValidator
23 {
24     /**
25      * {@inheritdoc}
26      */
27     public function validate($value, Constraint $constraint)
28     {
29         if (!$constraint instanceof Range) {
30             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Range');
31         }
32
33         if (null === $value) {
34             return;
35         }
36
37         if (!is_numeric($value) && !$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
38             if ($this->context instanceof ExecutionContextInterface) {
39                 $this->context->buildViolation($constraint->invalidMessage)
40                     ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
41                     ->setCode(Range::INVALID_CHARACTERS_ERROR)
42                     ->addViolation();
43             } else {
44                 $this->buildViolation($constraint->invalidMessage)
45                     ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
46                     ->setCode(Range::INVALID_CHARACTERS_ERROR)
47                     ->addViolation();
48             }
49
50             return;
51         }
52
53         $min = $constraint->min;
54         $max = $constraint->max;
55
56         // Convert strings to DateTimes if comparing another DateTime
57         // This allows to compare with any date/time value supported by
58         // the DateTime constructor:
59         // http://php.net/manual/en/datetime.formats.php
60         if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
61             if (is_string($min)) {
62                 $min = new \DateTime($min);
63             }
64
65             if (is_string($max)) {
66                 $max = new \DateTime($max);
67             }
68         }
69
70         if (null !== $constraint->max && $value > $max) {
71             if ($this->context instanceof ExecutionContextInterface) {
72                 $this->context->buildViolation($constraint->maxMessage)
73                     ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
74                     ->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
75                     ->setCode(Range::TOO_HIGH_ERROR)
76                     ->addViolation();
77             } else {
78                 $this->buildViolation($constraint->maxMessage)
79                     ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
80                     ->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
81                     ->setCode(Range::TOO_HIGH_ERROR)
82                     ->addViolation();
83             }
84
85             return;
86         }
87
88         if (null !== $constraint->min && $value < $min) {
89             if ($this->context instanceof ExecutionContextInterface) {
90                 $this->context->buildViolation($constraint->minMessage)
91                     ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
92                     ->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
93                     ->setCode(Range::TOO_LOW_ERROR)
94                     ->addViolation();
95             } else {
96                 $this->buildViolation($constraint->minMessage)
97                     ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
98                     ->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
99                     ->setCode(Range::TOO_LOW_ERROR)
100                     ->addViolation();
101             }
102         }
103     }
104 }