Skip to content

Commit b1a9477

Browse files
committed
[Validator] Added ObjectInitializer visitor
1 parent 7e3a41d commit b1a9477

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/Symfony/Component/Validator/Context/ExecutionContextManager.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class ExecutionContextManager extends AbstractVisitor implements ExecutionContex
4545
public function __construct(GroupManagerInterface $groupManager)
4646
{
4747
$this->groupManager = $groupManager;
48-
49-
$this->reset();
48+
$this->contextStack = new \SplStack();
5049
}
5150

5251
public function initialize(ValidatorInterface $validator)
@@ -95,7 +94,7 @@ public function getCurrentContext()
9594

9695
public function afterTraversal(array $nodes)
9796
{
98-
$this->reset();
97+
$this->contextStack = new \SplStack();
9998
}
10099

101100
public function enterNode(Node $node)
@@ -115,9 +114,4 @@ public function leaveNode(Node $node)
115114

116115
$this->currentContext->popNode();
117116
}
118-
119-
private function reset()
120-
{
121-
$this->contextStack = new \SplStack();
122-
}
123117
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\NodeVisitor;
13+
14+
use Symfony\Component\Validator\Node\ClassNode;
15+
use Symfony\Component\Validator\Node\Node;
16+
use Symfony\Component\Validator\ObjectInitializerInterface;
17+
18+
/**
19+
* @since %%NextVersion%%
20+
* @author Bernhard Schussek <[email protected]>
21+
*/
22+
class ObjectInitializer extends AbstractVisitor
23+
{
24+
/**
25+
* @var ObjectInitializerInterface[]
26+
*/
27+
private $initializers;
28+
29+
public function __construct(array $initializers)
30+
{
31+
foreach ($initializers as $initializer) {
32+
if (!$initializer instanceof ObjectInitializerInterface) {
33+
throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
34+
}
35+
}
36+
37+
$this->initializers = $initializers;
38+
}
39+
40+
public function enterNode(Node $node)
41+
{
42+
if ($node instanceof ClassNode) {
43+
foreach ($this->initializers as $initializer) {
44+
$initializer->initialize($node->value);
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)