diff --git a/src/Symfony/Component/Validator/NodeVisitor/ContextUpdateVisitor.php b/src/Symfony/Component/Validator/NodeVisitor/ContextUpdateVisitor.php deleted file mode 100644 index 03243960b1..0000000000 --- a/src/Symfony/Component/Validator/NodeVisitor/ContextUpdateVisitor.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\NodeVisitor; - -use Symfony\Component\Validator\Context\ExecutionContextInterface; -use Symfony\Component\Validator\Node\Node; - -/** - * Informs the execution context about the currently validated node. - * - * @since 2.5 - * @author Bernhard Schussek - */ -class ContextUpdateVisitor extends AbstractVisitor -{ - /** - * Updates the execution context. - * - * @param Node $node The current node - * @param ExecutionContextInterface $context The execution context - */ - public function visit(Node $node, ExecutionContextInterface $context) - { - $context->setValue($node->value); - $context->setMetadata($node->metadata); - $context->setPropertyPath($node->propertyPath); - } -} diff --git a/src/Symfony/Component/Validator/NodeVisitor/DefaultGroupReplacingVisitor.php b/src/Symfony/Component/Validator/NodeVisitor/DefaultGroupReplacingVisitor.php deleted file mode 100644 index 6d152edf9c..0000000000 --- a/src/Symfony/Component/Validator/NodeVisitor/DefaultGroupReplacingVisitor.php +++ /dev/null @@ -1,73 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Validator\NodeVisitor; - -use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\Constraints\GroupSequence; -use Symfony\Component\Validator\Context\ExecutionContextInterface; -use Symfony\Component\Validator\Node\ClassNode; -use Symfony\Component\Validator\Node\Node; - -/** - * Checks class nodes whether their "Default" group is replaced by a group - * sequence and adjusts the validation groups accordingly. - * - * If the "Default" group is replaced for a class node, and if the validated - * groups of the node contain the group "Default", that group is replaced by - * the group sequence specified in the class' metadata. - * - * @since 2.5 - * @author Bernhard Schussek - */ -class DefaultGroupReplacingVisitor extends AbstractVisitor -{ - /** - * Replaces the "Default" group in the node's groups by the class' group - * sequence. - * - * @param Node $node The current node - * @param ExecutionContextInterface $context The execution context - */ - public function visit(Node $node, ExecutionContextInterface $context) - { - if (!$node instanceof ClassNode) { - return; - } - - if ($node->metadata->hasGroupSequence()) { - // The group sequence is statically defined for the class - $groupSequence = $node->metadata->getGroupSequence(); - } elseif ($node->metadata->isGroupSequenceProvider()) { - // The group sequence is dynamically obtained from the validated - // object - /** @var \Symfony\Component\Validator\GroupSequenceProviderInterface $value */ - $groupSequence = $node->value->getGroupSequence(); - - if (!$groupSequence instanceof GroupSequence) { - $groupSequence = new GroupSequence($groupSequence); - } - } else { - // The "Default" group is not overridden. Quit. - return; - } - - $key = array_search(Constraint::DEFAULT_GROUP, $node->groups); - - if (false !== $key) { - // Replace the "Default" group by the group sequence - $node->groups[$key] = $groupSequence; - - // Cascade the "Default" group when validating the sequence - $groupSequence->cascadedGroup = Constraint::DEFAULT_GROUP; - } - } -} diff --git a/src/Symfony/Component/Validator/NodeVisitor/NodeValidationVisitor.php b/src/Symfony/Component/Validator/NodeVisitor/NodeValidationVisitor.php index 65e9bcc788..3a5a0f2bf8 100644 --- a/src/Symfony/Component/Validator/NodeVisitor/NodeValidationVisitor.php +++ b/src/Symfony/Component/Validator/NodeVisitor/NodeValidationVisitor.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\NodeVisitor; +use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\GroupSequence; use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; use Symfony\Component\Validator\Context\ExecutionContextInterface; @@ -68,7 +69,13 @@ class NodeValidationVisitor extends AbstractVisitor return true; } + $context->setValue($node->value); + $context->setMetadata($node->metadata); + $context->setPropertyPath($node->propertyPath); + if ($node instanceof ClassNode) { + $this->replaceDefaultGroup($node); + $objectHash = spl_object_hash($node->value); } elseif ($node instanceof PropertyNode) { $objectHash = spl_object_hash($node->object); @@ -203,4 +210,44 @@ class NodeValidationVisitor extends AbstractVisitor throw $e; } } + + /** + * Checks class nodes whether their "Default" group is replaced by a group + * sequence and adjusts the validation groups accordingly. + * + * If the "Default" group is replaced for a class node, and if the validated + * groups of the node contain the group "Default", that group is replaced by + * the group sequence specified in the class' metadata. + * + * @param ClassNode $node The node + */ + private function replaceDefaultGroup(ClassNode $node) + { + if ($node->metadata->hasGroupSequence()) { + // The group sequence is statically defined for the class + $groupSequence = $node->metadata->getGroupSequence(); + } elseif ($node->metadata->isGroupSequenceProvider()) { + // The group sequence is dynamically obtained from the validated + // object + /** @var \Symfony\Component\Validator\GroupSequenceProviderInterface $value */ + $groupSequence = $node->value->getGroupSequence(); + + if (!$groupSequence instanceof GroupSequence) { + $groupSequence = new GroupSequence($groupSequence); + } + } else { + // The "Default" group is not overridden. Quit. + return; + } + + $key = array_search(Constraint::DEFAULT_GROUP, $node->groups); + + if (false !== $key) { + // Replace the "Default" group by the group sequence + $node->groups[$key] = $groupSequence; + + // Cascade the "Default" group when validating the sequence + $groupSequence->cascadedGroup = Constraint::DEFAULT_GROUP; + } + } } diff --git a/src/Symfony/Component/Validator/Tests/Validator/TraversingValidator2Dot5ApiTest.php b/src/Symfony/Component/Validator/Tests/Validator/TraversingValidator2Dot5ApiTest.php index 61e78456b3..7f96944b77 100644 --- a/src/Symfony/Component/Validator/Tests/Validator/TraversingValidator2Dot5ApiTest.php +++ b/src/Symfony/Component/Validator/Tests/Validator/TraversingValidator2Dot5ApiTest.php @@ -29,13 +29,7 @@ class TraversingValidator2Dot5ApiTest extends Abstract2Dot5ApiTest $contextFactory = new ExecutionContextFactory(new DefaultTranslator()); $validator = new TraversingValidator($contextFactory, $nodeTraverser, $metadataFactory); - $groupSequenceResolver = new DefaultGroupReplacingVisitor(); - $contextRefresher = new ContextUpdateVisitor(); - $nodeValidator = new NodeValidationVisitor($nodeTraverser, new ConstraintValidatorFactory()); - - $nodeTraverser->addVisitor($groupSequenceResolver); - $nodeTraverser->addVisitor($contextRefresher); - $nodeTraverser->addVisitor($nodeValidator); + $nodeTraverser->addVisitor(new NodeValidationVisitor($nodeTraverser, new ConstraintValidatorFactory())); return $validator; }