4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
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;
20 * Provides a base class for the validation of property comparisons.
22 * @author Daniel Holmes <daniel@danielholmes.org>
23 * @author Bernhard Schussek <bschussek@gmail.com>
25 abstract class AbstractComparisonValidator extends ConstraintValidator
30 public function validate($value, Constraint $constraint)
32 if (!$constraint instanceof AbstractComparison) {
33 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
36 if (null === $value) {
40 $comparedValue = $constraint->value;
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);
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())
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())
77 * Compares the two given values to find if their relationship is valid.
79 * @param mixed $value1 The first value to compare
80 * @param mixed $value2 The second value to compare
82 * @return bool true if the relationship is valid, false otherwise
84 abstract protected function compareValues($value1, $value2);
87 * Returns the error code used if the comparison fails.
89 * @return string|null The error code or `null` if no code should be set
91 protected function getErrorCode()