Mercurial > hg > isophonics-drupal-site
diff 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 |
line wrap: on
line diff
--- a/vendor/symfony/validator/Constraints/AbstractComparisonValidator.php Mon Apr 23 09:33:26 2018 +0100 +++ b/vendor/symfony/validator/Constraints/AbstractComparisonValidator.php Mon Apr 23 09:46:53 2018 +0100 @@ -11,8 +11,12 @@ namespace Symfony\Component\Validator\Constraints; +use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; +use Symfony\Component\PropertyAccess\PropertyAccess; +use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** @@ -23,6 +27,13 @@ */ abstract class AbstractComparisonValidator extends ConstraintValidator { + private $propertyAccessor; + + public function __construct(PropertyAccessor $propertyAccessor = null) + { + $this->propertyAccessor = $propertyAccessor; + } + /** * {@inheritdoc} */ @@ -36,7 +47,19 @@ return; } - $comparedValue = $constraint->value; + if ($path = $constraint->propertyPath) { + if (null === $object = $this->context->getObject()) { + return; + } + + try { + $comparedValue = $this->getPropertyAccessor()->getValue($object, $path); + } catch (NoSuchPropertyException $e) { + throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: %s', $path, get_class($constraint), $e->getMessage()), 0, $e); + } + } else { + $comparedValue = $constraint->value; + } // Convert strings to DateTimes if comparing another DateTime // This allows to compare with any date/time value supported by @@ -46,7 +69,7 @@ if ($value instanceof \DateTimeImmutable) { // If $value is immutable, convert the compared value to a // DateTimeImmutable too - $comparedValue = new \DatetimeImmutable($comparedValue); + $comparedValue = new \DateTimeImmutable($comparedValue); } elseif ($value instanceof \DateTimeInterface) { // Otherwise use DateTime $comparedValue = new \DateTime($comparedValue); @@ -63,6 +86,15 @@ } } + private function getPropertyAccessor() + { + if (null === $this->propertyAccessor) { + $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); + } + + return $this->propertyAccessor; + } + /** * Compares the two given values to find if their relationship is valid. *