|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Validator\NodeVisitor; |
13 | 13 |
|
| 14 | +use Symfony\Component\Validator\Constraint; |
14 | 15 | use Symfony\Component\Validator\Constraints\GroupSequence; |
15 | 16 | use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; |
16 | 17 | use Symfony\Component\Validator\Context\ExecutionContextInterface; |
@@ -68,7 +69,13 @@ public function visit(Node $node, ExecutionContextInterface $context) |
68 | 69 | return true; |
69 | 70 | } |
70 | 71 |
|
| 72 | + $context->setValue($node->value); |
| 73 | + $context->setMetadata($node->metadata); |
| 74 | + $context->setPropertyPath($node->propertyPath); |
| 75 | + |
71 | 76 | if ($node instanceof ClassNode) { |
| 77 | + $this->replaceDefaultGroup($node); |
| 78 | + |
72 | 79 | $objectHash = spl_object_hash($node->value); |
73 | 80 | } elseif ($node instanceof PropertyNode) { |
74 | 81 | $objectHash = spl_object_hash($node->object); |
@@ -203,4 +210,44 @@ private function validateNodeForGroup(Node $node, $group, ExecutionContextInterf |
203 | 210 | throw $e; |
204 | 211 | } |
205 | 212 | } |
| 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 | + } |
206 | 253 | } |
0 commit comments