Skip to content

Commit be508e0

Browse files
committed
[Validator] Merged DefaultGroupReplacingVisitor and ContextUpdateVisitor into NodeValidationVisitor
1 parent 50bb84d commit be508e0

File tree

4 files changed

+48
-117
lines changed

4 files changed

+48
-117
lines changed

src/Symfony/Component/Validator/NodeVisitor/ContextUpdateVisitor.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Symfony/Component/Validator/NodeVisitor/DefaultGroupReplacingVisitor.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/Symfony/Component/Validator/NodeVisitor/NodeValidationVisitor.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\NodeVisitor;
1313

14+
use Symfony\Component\Validator\Constraint;
1415
use Symfony\Component\Validator\Constraints\GroupSequence;
1516
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
1617
use Symfony\Component\Validator\Context\ExecutionContextInterface;
@@ -68,7 +69,13 @@ public function visit(Node $node, ExecutionContextInterface $context)
6869
return true;
6970
}
7071

72+
$context->setValue($node->value);
73+
$context->setMetadata($node->metadata);
74+
$context->setPropertyPath($node->propertyPath);
75+
7176
if ($node instanceof ClassNode) {
77+
$this->replaceDefaultGroup($node);
78+
7279
$objectHash = spl_object_hash($node->value);
7380
} elseif ($node instanceof PropertyNode) {
7481
$objectHash = spl_object_hash($node->object);
@@ -203,4 +210,44 @@ private function validateNodeForGroup(Node $node, $group, ExecutionContextInterf
203210
throw $e;
204211
}
205212
}
213+
214+
/**
215+
* Checks class nodes whether their "Default" group is replaced by a group
216+
* sequence and adjusts the validation groups accordingly.
217+
*
218+
* If the "Default" group is replaced for a class node, and if the validated
219+
* groups of the node contain the group "Default", that group is replaced by
220+
* the group sequence specified in the class' metadata.
221+
*
222+
* @param ClassNode $node The node
223+
*/
224+
private function replaceDefaultGroup(ClassNode $node)
225+
{
226+
if ($node->metadata->hasGroupSequence()) {
227+
// The group sequence is statically defined for the class
228+
$groupSequence = $node->metadata->getGroupSequence();
229+
} elseif ($node->metadata->isGroupSequenceProvider()) {
230+
// The group sequence is dynamically obtained from the validated
231+
// object
232+
/** @var \Symfony\Component\Validator\GroupSequenceProviderInterface $value */
233+
$groupSequence = $node->value->getGroupSequence();
234+
235+
if (!$groupSequence instanceof GroupSequence) {
236+
$groupSequence = new GroupSequence($groupSequence);
237+
}
238+
} else {
239+
// The "Default" group is not overridden. Quit.
240+
return;
241+
}
242+
243+
$key = array_search(Constraint::DEFAULT_GROUP, $node->groups);
244+
245+
if (false !== $key) {
246+
// Replace the "Default" group by the group sequence
247+
$node->groups[$key] = $groupSequence;
248+
249+
// Cascade the "Default" group when validating the sequence
250+
$groupSequence->cascadedGroup = Constraint::DEFAULT_GROUP;
251+
}
252+
}
206253
}

src/Symfony/Component/Validator/Tests/Validator/TraversingValidator2Dot5ApiTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ protected function createValidator(MetadataFactoryInterface $metadataFactory)
2929
$contextFactory = new ExecutionContextFactory(new DefaultTranslator());
3030
$validator = new TraversingValidator($contextFactory, $nodeTraverser, $metadataFactory);
3131

32-
$groupSequenceResolver = new DefaultGroupReplacingVisitor();
33-
$contextRefresher = new ContextUpdateVisitor();
34-
$nodeValidator = new NodeValidationVisitor($nodeTraverser, new ConstraintValidatorFactory());
35-
36-
$nodeTraverser->addVisitor($groupSequenceResolver);
37-
$nodeTraverser->addVisitor($contextRefresher);
38-
$nodeTraverser->addVisitor($nodeValidator);
32+
$nodeTraverser->addVisitor(new NodeValidationVisitor($nodeTraverser, new ConstraintValidatorFactory()));
3933

4034
return $validator;
4135
}

0 commit comments

Comments
 (0)