[Validator] Merged DefaultGroupReplacingVisitor and ContextUpdateVisitor into NodeValidationVisitor

This commit is contained in:
Bernhard Schussek 2014-03-11 14:56:15 +01:00
parent 50bb84d06b
commit be508e01dd
4 changed files with 48 additions and 117 deletions

View File

@ -1,37 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <bschussek@gmail.com>
*/
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);
}
}

View File

@ -1,73 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <bschussek@gmail.com>
*/
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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}