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\PropertyAccess\Exception\NoSuchPropertyException;
15 use Symfony\Component\PropertyAccess\PropertyAccess;
16 use Symfony\Component\PropertyAccess\PropertyAccessor;
17 use Symfony\Component\Validator\Constraint;
18 use Symfony\Component\Validator\ConstraintValidator;
19 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
20 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
23 * Provides a base class for the validation of property comparisons.
25 * @author Daniel Holmes <daniel@danielholmes.org>
26 * @author Bernhard Schussek <bschussek@gmail.com>
28 abstract class AbstractComparisonValidator extends ConstraintValidator
30 private $propertyAccessor;
32 public function __construct(PropertyAccessor $propertyAccessor = null)
34 $this->propertyAccessor = $propertyAccessor;
40 public function validate($value, Constraint $constraint)
42 if (!$constraint instanceof AbstractComparison) {
43 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
46 if (null === $value) {
50 if ($path = $constraint->propertyPath) {
51 if (null === $object = $this->context->getObject()) {
56 $comparedValue = $this->getPropertyAccessor()->getValue($object, $path);
57 } catch (NoSuchPropertyException $e) {
58 throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: %s', $path, \get_class($constraint), $e->getMessage()), 0, $e);
61 $comparedValue = $constraint->value;
64 // Convert strings to DateTimes if comparing another DateTime
65 // This allows to compare with any date/time value supported by
66 // the DateTime constructor:
67 // http://php.net/manual/en/datetime.formats.php
68 if (\is_string($comparedValue)) {
69 if ($value instanceof \DateTimeImmutable) {
70 // If $value is immutable, convert the compared value to a
71 // DateTimeImmutable too
72 $comparedValue = new \DateTimeImmutable($comparedValue);
73 } elseif ($value instanceof \DateTimeInterface) {
74 // Otherwise use DateTime
75 $comparedValue = new \DateTime($comparedValue);
79 if (!$this->compareValues($value, $comparedValue)) {
80 $this->context->buildViolation($constraint->message)
81 ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
82 ->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
83 ->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
84 ->setCode($this->getErrorCode())
89 private function getPropertyAccessor()
91 if (null === $this->propertyAccessor) {
92 $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
95 return $this->propertyAccessor;
99 * Compares the two given values to find if their relationship is valid.
101 * @param mixed $value1 The first value to compare
102 * @param mixed $value2 The second value to compare
104 * @return bool true if the relationship is valid, false otherwise
106 abstract protected function compareValues($value1, $value2);
109 * Returns the error code used if the comparison fails.
111 * @return string|null The error code or `null` if no code should be set
113 protected function getErrorCode()