From 499eeb4ef762a62a53ebe5ed6d6bce6191d4e2f7 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 26 Sep 2014 12:44:24 +0200 Subject: [PATCH] [Validator] Made it possible to store the cause of a constraint violation --- src/Symfony/Component/Validator/CHANGELOG.md | 1 + .../Validator/ConstraintViolation.php | 25 ++++++++++++++++--- .../AbstractConstraintValidatorTest.php | 11 +++++++- .../Violation/ConstraintViolationBuilder.php | 18 ++++++++++++- .../ConstraintViolationBuilderInterface.php | 9 +++++++ .../LegacyConstraintViolationBuilder.php | 15 +++++++---- 6 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 753b4426986c..329eaf3eb0ae 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -14,6 +14,7 @@ CHANGELOG * deprecated `ClassMetadata::addMemberMetadata()` * [BC BREAK] added `Mapping\MetadataInterface::getConstraints()` * added generic "payload" option to all constraints for attaching domain-specific data + * [BC BREAK] added `ConstraintViolationBuilderInterface::setCause()` 2.5.0 ----- diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index 5cf57ed4f8dc..8f4744f576fe 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -63,6 +63,11 @@ class ConstraintViolation implements ConstraintViolationInterface */ private $code; + /** + * @var mixed + */ + private $cause; + /** * Creates a new constraint violation. * @@ -79,10 +84,11 @@ class ConstraintViolation implements ConstraintViolationInterface * @param int|null $plural The number for determining the plural * form when translating the message * @param mixed $code The error code of the violation - * @param Constraint|null $constraint The constraint that caused the - * violation + * @param Constraint|null $constraint The constraint whose validation + * caused the violation + * @param mixed $cause The cause of the violation */ - public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null) + public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null, $cause = null) { $this->message = $message; $this->messageTemplate = $messageTemplate; @@ -93,6 +99,7 @@ public function __construct($message, $messageTemplate, array $parameters, $root $this->invalidValue = $invalidValue; $this->constraint = $constraint; $this->code = $code; + $this->cause = $cause; } /** @@ -197,7 +204,7 @@ public function getInvalidValue() } /** - * Returns the constraint that caused the violation. + * Returns the constraint whose validation caused the violation. * * @return Constraint|null The constraint or null if it is not known */ @@ -206,6 +213,16 @@ public function getConstraint() return $this->constraint; } + /** + * Returns the cause of the violation. + * + * @return mixed + */ + public function getCause() + { + return $this->cause; + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php index 533c6cd9527a..762dac553be0 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php @@ -428,6 +428,7 @@ class ConstraintViolationAssertion private $plural; private $code; private $constraint; + private $cause; public function __construct(LegacyExecutionContextInterface $context, $message, Constraint $constraint = null, array $assertions = array()) { @@ -486,6 +487,13 @@ public function setCode($code) return $this; } + public function setCause($cause) + { + $this->cause = $cause; + + return $this; + } + public function buildNextViolation($message) { $assertions = $this->assertions; @@ -525,7 +533,8 @@ private function getViolation() $this->invalidValue, $this->plural, $this->code, - $this->constraint + $this->constraint, + $this->cause ); } } diff --git a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php index 6d6bd0277446..b752c397b880 100644 --- a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php +++ b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php @@ -83,6 +83,11 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface */ private $code; + /** + * @var mixed + */ + private $cause; + public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null) { $this->violations = $violations; @@ -166,6 +171,16 @@ public function setCode($code) return $this; } + /** + * {@inheritdoc} + */ + public function setCause($cause) + { + $this->cause = $cause; + + return $this; + } + /** * {@inheritdoc} */ @@ -203,7 +218,8 @@ public function addViolation() $this->invalidValue, $this->plural, $this->code, - $this->constraint + $this->constraint, + $this->cause )); } } diff --git a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php index 84cd4d32548d..9cb3fdb7e6ef 100644 --- a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php +++ b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilderInterface.php @@ -101,6 +101,15 @@ public function setPlural($number); */ public function setCode($code); + /** + * Sets the cause of the violation. + * + * @param mixed $cause The cause of the violation + * + * @return ConstraintViolationBuilderInterface This builder + */ + public function setCause($cause); + /** * Adds the violation to the current execution context. */ diff --git a/src/Symfony/Component/Validator/Violation/LegacyConstraintViolationBuilder.php b/src/Symfony/Component/Validator/Violation/LegacyConstraintViolationBuilder.php index 06cfdb60fbd5..0607552cfc88 100644 --- a/src/Symfony/Component/Validator/Violation/LegacyConstraintViolationBuilder.php +++ b/src/Symfony/Component/Validator/Violation/LegacyConstraintViolationBuilder.php @@ -11,12 +11,7 @@ namespace Symfony\Component\Validator\Violation; -use Symfony\Component\Translation\TranslatorInterface; -use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\ConstraintViolation; -use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ExecutionContextInterface; -use Symfony\Component\Validator\Util\PropertyPath; /** * Backwards-compatible implementation of {@link ConstraintViolationBuilderInterface}. @@ -149,6 +144,16 @@ public function setCode($code) return $this; } + /** + * {@inheritdoc} + */ + public function setCause($cause) + { + // do nothing - we can't save the cause through the old API + + return $this; + } + /** * {@inheritdoc} */