Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/validator/Constraints/AbstractComparisonValidator.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
9 * file that was distributed with this source code. | 9 * file that was distributed with this source code. |
10 */ | 10 */ |
11 | 11 |
12 namespace Symfony\Component\Validator\Constraints; | 12 namespace Symfony\Component\Validator\Constraints; |
13 | 13 |
14 use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; | |
15 use Symfony\Component\PropertyAccess\PropertyAccess; | |
16 use Symfony\Component\PropertyAccess\PropertyAccessor; | |
14 use Symfony\Component\Validator\Constraint; | 17 use Symfony\Component\Validator\Constraint; |
15 use Symfony\Component\Validator\ConstraintValidator; | 18 use Symfony\Component\Validator\ConstraintValidator; |
19 use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | |
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException; | 20 use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
17 | 21 |
18 /** | 22 /** |
19 * Provides a base class for the validation of property comparisons. | 23 * Provides a base class for the validation of property comparisons. |
20 * | 24 * |
21 * @author Daniel Holmes <daniel@danielholmes.org> | 25 * @author Daniel Holmes <daniel@danielholmes.org> |
22 * @author Bernhard Schussek <bschussek@gmail.com> | 26 * @author Bernhard Schussek <bschussek@gmail.com> |
23 */ | 27 */ |
24 abstract class AbstractComparisonValidator extends ConstraintValidator | 28 abstract class AbstractComparisonValidator extends ConstraintValidator |
25 { | 29 { |
30 private $propertyAccessor; | |
31 | |
32 public function __construct(PropertyAccessor $propertyAccessor = null) | |
33 { | |
34 $this->propertyAccessor = $propertyAccessor; | |
35 } | |
36 | |
26 /** | 37 /** |
27 * {@inheritdoc} | 38 * {@inheritdoc} |
28 */ | 39 */ |
29 public function validate($value, Constraint $constraint) | 40 public function validate($value, Constraint $constraint) |
30 { | 41 { |
34 | 45 |
35 if (null === $value) { | 46 if (null === $value) { |
36 return; | 47 return; |
37 } | 48 } |
38 | 49 |
39 $comparedValue = $constraint->value; | 50 if ($path = $constraint->propertyPath) { |
51 if (null === $object = $this->context->getObject()) { | |
52 return; | |
53 } | |
54 | |
55 try { | |
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); | |
59 } | |
60 } else { | |
61 $comparedValue = $constraint->value; | |
62 } | |
40 | 63 |
41 // Convert strings to DateTimes if comparing another DateTime | 64 // Convert strings to DateTimes if comparing another DateTime |
42 // This allows to compare with any date/time value supported by | 65 // This allows to compare with any date/time value supported by |
43 // the DateTime constructor: | 66 // the DateTime constructor: |
44 // http://php.net/manual/en/datetime.formats.php | 67 // http://php.net/manual/en/datetime.formats.php |
45 if (is_string($comparedValue)) { | 68 if (is_string($comparedValue)) { |
46 if ($value instanceof \DateTimeImmutable) { | 69 if ($value instanceof \DateTimeImmutable) { |
47 // If $value is immutable, convert the compared value to a | 70 // If $value is immutable, convert the compared value to a |
48 // DateTimeImmutable too | 71 // DateTimeImmutable too |
49 $comparedValue = new \DatetimeImmutable($comparedValue); | 72 $comparedValue = new \DateTimeImmutable($comparedValue); |
50 } elseif ($value instanceof \DateTimeInterface) { | 73 } elseif ($value instanceof \DateTimeInterface) { |
51 // Otherwise use DateTime | 74 // Otherwise use DateTime |
52 $comparedValue = new \DateTime($comparedValue); | 75 $comparedValue = new \DateTime($comparedValue); |
53 } | 76 } |
54 } | 77 } |
59 ->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE)) | 82 ->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE)) |
60 ->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue)) | 83 ->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue)) |
61 ->setCode($this->getErrorCode()) | 84 ->setCode($this->getErrorCode()) |
62 ->addViolation(); | 85 ->addViolation(); |
63 } | 86 } |
87 } | |
88 | |
89 private function getPropertyAccessor() | |
90 { | |
91 if (null === $this->propertyAccessor) { | |
92 $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); | |
93 } | |
94 | |
95 return $this->propertyAccessor; | |
64 } | 96 } |
65 | 97 |
66 /** | 98 /** |
67 * Compares the two given values to find if their relationship is valid. | 99 * Compares the two given values to find if their relationship is valid. |
68 * | 100 * |