From 718601c6c3642fa7a65a408016a34feef00fe44d Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 19 Feb 2014 12:02:54 +0100 Subject: [PATCH] [Validator] Changed validateValue() to validate() in the new API The validation of values against constraints should be a first-class citizen in the API. For this reason, validate() now takes a value and a constraint or a list of constraints. This method should be used for the most basic use cases. If users want to annotate objects with constraints (this is optional, advanced functionality), they can use the more expressive validateObject() method now. For traversing arrays or Traversables, a new method validateCollection() is now available in the API. --- .../Context/LegacyExecutionContext.php | 6 ++-- .../Validator/ContextualValidator.php | 34 +++++++++---------- .../Validator/Validator/LegacyValidator.php | 11 ++++++ .../Validator/Validator/Validator.php | 19 +++++------ .../Validator/ValidatorInterface.php | 24 ++++++------- 5 files changed, 52 insertions(+), 42 deletions(-) diff --git a/src/Symfony/Component/Validator/Context/LegacyExecutionContext.php b/src/Symfony/Component/Validator/Context/LegacyExecutionContext.php index db30eb92b5..0563bdab3d 100644 --- a/src/Symfony/Component/Validator/Context/LegacyExecutionContext.php +++ b/src/Symfony/Component/Validator/Context/LegacyExecutionContext.php @@ -112,7 +112,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution ->getValidator() ->inContext($this) ->atPath($subPath) - ->validateValue($value, $constraint, $groups) + ->validate($value, $constraint, $groups) ; } @@ -126,7 +126,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution ->getValidator() ->inContext($this) ->atPath($subPath) - ->validateValue($value, $constraints, $groups) + ->validate($value, $constraints, $groups) ; } @@ -147,7 +147,7 @@ class LegacyExecutionContext extends ExecutionContext implements LegacyExecution ->getValidator() ->inContext($this) ->atPath($subPath) - ->validateValue($value, $constraints, $groups) + ->validate($value, $constraints, $groups) ; } diff --git a/src/Symfony/Component/Validator/Validator/ContextualValidator.php b/src/Symfony/Component/Validator/Validator/ContextualValidator.php index 8a58f48d9f..a3e21863df 100644 --- a/src/Symfony/Component/Validator/Validator/ContextualValidator.php +++ b/src/Symfony/Component/Validator/Validator/ContextualValidator.php @@ -46,6 +46,23 @@ class ContextualValidator extends AbstractValidator implements ContextualValidat return $this; } + /** + * Validates a value against a constraint or a list of constraints. + * + * @param mixed $value The value to validate. + * @param Constraint|Constraint[] $constraints The constraint(s) to validate against. + * @param array|null $groups The validation groups to validate. + * + * @return ConstraintViolationListInterface A list of constraint violations. If the + * list is empty, validation succeeded. + */ + public function validate($value, $constraints, $groups = null) + { + $this->traverseValue($value, $constraints, $groups); + + return $this->context->getViolations(); + } + /** * Validates a value. * @@ -118,21 +135,4 @@ class ContextualValidator extends AbstractValidator implements ContextualValidat return $this->context->getViolations(); } - - /** - * Validates a value against a constraint or a list of constraints. - * - * @param mixed $value The value to validate. - * @param Constraint|Constraint[] $constraints The constraint(s) to validate against. - * @param array|null $groups The validation groups to validate. - * - * @return ConstraintViolationListInterface A list of constraint violations. If the - * list is empty, validation succeeded. - */ - public function validateValue($value, $constraints, $groups = null) - { - $this->traverseValue($value, $constraints, $groups); - - return $this->context->getViolations(); - } } diff --git a/src/Symfony/Component/Validator/Validator/LegacyValidator.php b/src/Symfony/Component/Validator/Validator/LegacyValidator.php index 059d84c1da..64df69e1e2 100644 --- a/src/Symfony/Component/Validator/Validator/LegacyValidator.php +++ b/src/Symfony/Component/Validator/Validator/LegacyValidator.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Validator; +use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\Traverse; use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface; @@ -23,6 +24,11 @@ class LegacyValidator extends Validator implements LegacyValidatorInterface { public function validate($value, $groups = null, $traverse = false, $deep = false) { + // Use new signature if constraints are given in the second argument + if (func_num_args() <= 3 && ($groups instanceof Constraint || (is_array($groups) && current($groups) instanceof Constraint))) { + return parent::validate($value, $groups, $traverse); + } + if (is_array($value)) { $constraint = new Traverse(array( 'traverse' => true, @@ -44,6 +50,11 @@ class LegacyValidator extends Validator implements LegacyValidatorInterface return $this->validateObject($value, $groups); } + public function validateValue($value, $constraints, $groups = null) + { + return parent::validate($value, $constraints, $groups); + } + public function getMetadataFactory() { return $this->metadataFactory; diff --git a/src/Symfony/Component/Validator/Validator/Validator.php b/src/Symfony/Component/Validator/Validator/Validator.php index 18022ba19f..5d8b509be3 100644 --- a/src/Symfony/Component/Validator/Validator/Validator.php +++ b/src/Symfony/Component/Validator/Validator/Validator.php @@ -34,6 +34,15 @@ class Validator extends AbstractValidator $this->contextManager = $contextManager; } + public function validate($value, $constraints, $groups = null) + { + $this->contextManager->startContext($value); + + $this->traverseValue($value, $constraints, $groups); + + return $this->contextManager->stopContext()->getViolations(); + } + public function validateObject($object, $groups = null) { $this->contextManager->startContext($object); @@ -74,14 +83,4 @@ class Validator extends AbstractValidator return $this->contextManager->stopContext()->getViolations(); } - - public function validateValue($value, $constraints, $groups = null) - { - $this->contextManager->startContext($value); - - $this->traverseValue($value, $constraints, $groups); - - return $this->contextManager->stopContext()->getViolations(); - } - } diff --git a/src/Symfony/Component/Validator/Validator/ValidatorInterface.php b/src/Symfony/Component/Validator/Validator/ValidatorInterface.php index 6d1833d162..8cecaf3dc4 100644 --- a/src/Symfony/Component/Validator/Validator/ValidatorInterface.php +++ b/src/Symfony/Component/Validator/Validator/ValidatorInterface.php @@ -21,6 +21,18 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; */ interface ValidatorInterface { + /** + * Validates a value against a constraint or a list of constraints. + * + * @param mixed $value The value to validate. + * @param Constraint|Constraint[] $constraints The constraint(s) to validate against. + * @param array|null $groups The validation groups to validate. + * + * @return ConstraintViolationListInterface A list of constraint violations. If the + * list is empty, validation succeeded. + */ + public function validate($value, $constraints, $groups = null); + /** * Validates a value. * @@ -69,18 +81,6 @@ interface ValidatorInterface */ public function validatePropertyValue($object, $propertyName, $value, $groups = null); - /** - * Validates a value against a constraint or a list of constraints. - * - * @param mixed $value The value to validate. - * @param Constraint|Constraint[] $constraints The constraint(s) to validate against. - * @param array|null $groups The validation groups to validate. - * - * @return ConstraintViolationListInterface A list of constraint violations. If the - * list is empty, validation succeeded. - */ - public function validateValue($value, $constraints, $groups = null); - /** * @param ExecutionContextInterface $context *