Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator\Constraints;
|
Chris@0
|
13
|
Chris@14
|
14 use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
|
Chris@14
|
15 use Symfony\Component\PropertyAccess\PropertyAccess;
|
Chris@18
|
16 use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
Chris@0
|
17 use Symfony\Component\Validator\Constraint;
|
Chris@0
|
18 use Symfony\Component\Validator\ConstraintValidator;
|
Chris@14
|
19 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
Chris@0
|
20 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Provides a base class for the validation of property comparisons.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Daniel Holmes <daniel@danielholmes.org>
|
Chris@0
|
26 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
27 */
|
Chris@0
|
28 abstract class AbstractComparisonValidator extends ConstraintValidator
|
Chris@0
|
29 {
|
Chris@14
|
30 private $propertyAccessor;
|
Chris@14
|
31
|
Chris@18
|
32 public function __construct(PropertyAccessorInterface $propertyAccessor = null)
|
Chris@14
|
33 {
|
Chris@14
|
34 $this->propertyAccessor = $propertyAccessor;
|
Chris@14
|
35 }
|
Chris@14
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * {@inheritdoc}
|
Chris@0
|
39 */
|
Chris@0
|
40 public function validate($value, Constraint $constraint)
|
Chris@0
|
41 {
|
Chris@0
|
42 if (!$constraint instanceof AbstractComparison) {
|
Chris@0
|
43 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 if (null === $value) {
|
Chris@0
|
47 return;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@14
|
50 if ($path = $constraint->propertyPath) {
|
Chris@14
|
51 if (null === $object = $this->context->getObject()) {
|
Chris@14
|
52 return;
|
Chris@14
|
53 }
|
Chris@14
|
54
|
Chris@14
|
55 try {
|
Chris@14
|
56 $comparedValue = $this->getPropertyAccessor()->getValue($object, $path);
|
Chris@14
|
57 } catch (NoSuchPropertyException $e) {
|
Chris@17
|
58 throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: %s', $path, \get_class($constraint), $e->getMessage()), 0, $e);
|
Chris@14
|
59 }
|
Chris@14
|
60 } else {
|
Chris@14
|
61 $comparedValue = $constraint->value;
|
Chris@14
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 // Convert strings to DateTimes if comparing another DateTime
|
Chris@0
|
65 // This allows to compare with any date/time value supported by
|
Chris@0
|
66 // the DateTime constructor:
|
Chris@0
|
67 // http://php.net/manual/en/datetime.formats.php
|
Chris@17
|
68 if (\is_string($comparedValue)) {
|
Chris@0
|
69 if ($value instanceof \DateTimeImmutable) {
|
Chris@0
|
70 // If $value is immutable, convert the compared value to a
|
Chris@0
|
71 // DateTimeImmutable too
|
Chris@14
|
72 $comparedValue = new \DateTimeImmutable($comparedValue);
|
Chris@0
|
73 } elseif ($value instanceof \DateTimeInterface) {
|
Chris@0
|
74 // Otherwise use DateTime
|
Chris@0
|
75 $comparedValue = new \DateTime($comparedValue);
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 if (!$this->compareValues($value, $comparedValue)) {
|
Chris@0
|
80 $this->context->buildViolation($constraint->message)
|
Chris@0
|
81 ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
|
Chris@0
|
82 ->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
|
Chris@0
|
83 ->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
|
Chris@0
|
84 ->setCode($this->getErrorCode())
|
Chris@0
|
85 ->addViolation();
|
Chris@0
|
86 }
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@14
|
89 private function getPropertyAccessor()
|
Chris@14
|
90 {
|
Chris@14
|
91 if (null === $this->propertyAccessor) {
|
Chris@14
|
92 $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
|
Chris@14
|
93 }
|
Chris@14
|
94
|
Chris@14
|
95 return $this->propertyAccessor;
|
Chris@14
|
96 }
|
Chris@14
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * Compares the two given values to find if their relationship is valid.
|
Chris@0
|
100 *
|
Chris@0
|
101 * @param mixed $value1 The first value to compare
|
Chris@0
|
102 * @param mixed $value2 The second value to compare
|
Chris@0
|
103 *
|
Chris@0
|
104 * @return bool true if the relationship is valid, false otherwise
|
Chris@0
|
105 */
|
Chris@0
|
106 abstract protected function compareValues($value1, $value2);
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Returns the error code used if the comparison fails.
|
Chris@0
|
110 *
|
Chris@0
|
111 * @return string|null The error code or `null` if no code should be set
|
Chris@0
|
112 */
|
Chris@0
|
113 protected function getErrorCode()
|
Chris@0
|
114 {
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|