Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / symfony / validator / Constraints / AbstractComparisonValidator.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  * Provides a base class for the validation of property comparisons.
21  *
22  * @author Daniel Holmes <daniel@danielholmes.org>
23  * @author Bernhard Schussek <bschussek@gmail.com>
24  */
25 abstract class AbstractComparisonValidator extends ConstraintValidator
26 {
27     /**
28      * {@inheritdoc}
29      */
30     public function validate($value, Constraint $constraint)
31     {
32         if (!$constraint instanceof AbstractComparison) {
33             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
34         }
35
36         if (null === $value) {
37             return;
38         }
39
40         $comparedValue = $constraint->value;
41
42         // Convert strings to DateTimes if comparing another DateTime
43         // This allows to compare with any date/time value supported by
44         // the DateTime constructor:
45         // http://php.net/manual/en/datetime.formats.php
46         if (is_string($comparedValue)) {
47             if ($value instanceof \DateTimeImmutable) {
48                 // If $value is immutable, convert the compared value to a
49                 // DateTimeImmutable too
50                 $comparedValue = new \DatetimeImmutable($comparedValue);
51             } elseif ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
52                 // Otherwise use DateTime
53                 $comparedValue = new \DateTime($comparedValue);
54             }
55         }
56
57         if (!$this->compareValues($value, $comparedValue)) {
58             if ($this->context instanceof ExecutionContextInterface) {
59                 $this->context->buildViolation($constraint->message)
60                     ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
61                     ->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
62                     ->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
63                     ->setCode($this->getErrorCode())
64                     ->addViolation();
65             } else {
66                 $this->buildViolation($constraint->message)
67                     ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
68                     ->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
69                     ->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
70                     ->setCode($this->getErrorCode())
71                     ->addViolation();
72             }
73         }
74     }
75
76     /**
77      * Compares the two given values to find if their relationship is valid.
78      *
79      * @param mixed $value1 The first value to compare
80      * @param mixed $value2 The second value to compare
81      *
82      * @return bool true if the relationship is valid, false otherwise
83      */
84     abstract protected function compareValues($value1, $value2);
85
86     /**
87      * Returns the error code used if the comparison fails.
88      *
89      * @return string|null The error code or `null` if no code should be set
90      */
91     protected function getErrorCode()
92     {
93     }
94 }