23
23
use Symfony \Component \Validator \Violation \ConstraintViolationBuilder ;
24
24
25
25
/**
26
- * @since %%NextVersion%%
26
+ * The context used and created by {@link ExecutionContextManager}.
27
+ *
28
+ * @since 2.5
27
29
* @author Bernhard Schussek <bschussek@gmail.com>
30
+ *
31
+ * @see ExecutionContextInterface
28
32
*/
29
33
class ExecutionContext implements ExecutionContextInterface
30
34
{
35
+ /**
36
+ * The root value of the validated object graph.
37
+ *
38
+ * @var mixed
39
+ */
31
40
private $ root ;
32
41
42
+ /**
43
+ * The violations generated in the current context.
44
+ *
45
+ * @var ConstraintViolationList
46
+ */
33
47
private $ violations ;
34
48
35
49
/**
<
8000
td data-grid-cell-id="diff-ffba2bed571e18a91782556cc0ca76152bcb556bd1a83590e733f4784e7c88f4-35-50-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
50
+ * The current node under validation.
51
+ *
36
52
* @var Node
37
53
*/
38
54
private $ node ;
39
55
40
56
/**
57
+ * The trace of nodes from the root node to the current node.
58
+ *
41
59
* @var \SplStack
42
60
*/
43
61
private $ nodeStack ;
@@ -73,25 +91,39 @@ public function __construct($root, ValidatorInterface $validator, GroupManagerIn
73
91
$ this ->nodeStack = new \SplStack ();
74
92
}
75
93
94
+ /**
95
+ * Sets the values of the context to match the given node.
96
+ *
97
+ * Internally, all nodes are stored on a stack and can be removed from that
98
+ * stack using {@link popNode()}.
99
+ *
100
+ * @param Node $node The currently validated node
101
+ */
76
102
public function pushNode (Node $ node )
77
103
{
78
- if (null !== $ this ->node ) {
79
- $ this ->nodeStack ->push ($ this ->node );
80
- }
81
-
104
+ $ this ->nodeStack ->push ($ node );
82
105
$ this ->node = $ node ;
83
106
}
84
107
108
+ /**
109
+ * Sets the values of the context to match the previous node.
110
+ *
111
+ * The current node is removed from the internal stack and returned.
112
+ *
113
+ * @return Node|null The currently validated node or null, if no node was
114
+ * on the stack
115
+ */
85
116
public function popNode ()
86
117
{
87
- $ poppedNode = $ this ->node ;
88
-
118
+ // Nothing to do if the stack is empty
89
119
if (0 === count ($ this ->nodeStack )) {
90
- $ this ->node = null ;
91
-
92
- return $ poppedNode ;
120
+ return null ;
93
121
}
94
122
123
+ $ poppedNode = $ this ->node ;
124
+
125
+ // After removing the last node, the stack is empty and the node
126
+ // is null
95
127
if (1 === count ($ this ->nodeStack )) {
96
128
$ this ->nodeStack ->pop ();
97
129
$ this ->node = null ;
@@ -105,6 +137,9 @@ public function popNode()
105
137
return $ poppedNode ;
106
138
}
107
139
140
+ /**
141
+ * {@inheritdoc}
142
+ */
108
143
public function addViolation ($ message , array $ parameters = array ())
109
144
{
110
145
$ this ->violations ->add (new ConstraintViolation (
@@ -119,6 +154,9 @@ public function addViolation($message, array $parameters = array())
119
154
));
120
155
}
121
156
157
+ /**
158
+ * {@inheritdoc}
159
+ */
122
160
public function buildViolation ($ message , array $ parameters = array ())
123
161
{
124
162
return new ConstraintViolationBuilder (
@@ -133,57 +171,81 @@ public function buildViolation($message, array $parameters = array())
133
171
);
134
172
}
135
173
174
+ /**
175
+ * {@inheritdoc}
176
+ */
136
177
public function getViolations ()
137
178
{
138
179
return $ this ->violations ;
139
180
}
140
181
182
+ /**
183
+ * {@inheritdoc}
184
+ */
185
+ public function getValidator ()
186
+ {
187
+ return $ this ->validator ;
188
+ }
189
+
190
+ /**
191
+ * {@inheritdoc}
192
+ */
141
193
public function getRoot ()
142
194
{
143
195
return $ this ->root ;
144
196
}
145
197
198
+ /**
199
+ * {@inheritdoc}
200
+ */
146
201
public function getValue ()
147
202
{
148
203
return $ this ->node ? $ this ->node ->value : null ;
149
204
}
150
205
206
+ /**
207
+ * {@inheritdoc}
208
+ */
151
209
public function getMetadata ()
152
210
{
153
211
return $ this ->node ? $ this ->node ->metadata : null ;
154
212
}
155
213
214
+ /**
215
+ * {@inheritdoc}
216
+ */
156
217
public function getGroup ()
157
218
{
158
219
return $ this ->groupManager ->getCurrentGroup ();
159
220
}
160
221
222
+ /**
223
+ * {@inheritdoc}
224
+ */
161
225
public function getClassName ()
162
226
{
163
227
$ metadata = $ this ->getMetadata ();
164
228
165
229
return $ metadata instanceof ClassBasedInterface ? $ metadata ->getClassName () : null ;
166
230
}
167
231
232
+ /**
233
+ * {@inheritdoc}
234
+ */
168
235
public function getPropertyName ()
169
236
{
170
237
$ metadata = $ this ->getMetadata ();
171
238
172
239
return $ metadata instanceof PropertyMetadataInterface ? $ metadata ->getPropertyName () : null ;
173
240
}
174
241
242
+ /**
243
+ * {@inheritdoc}
244
+ */
175
245
public function getPropertyPath ($ subPath = '' )
176
246
{
177
247
$ propertyPath = $ this ->node ? $ this ->node ->propertyPath : '' ;
178
248
179
249
return PropertyPath::append ($ propertyPath , $ subPath );
180
250
}
181
-
182
- /**
183
- * @return ValidatorInterface
184
- */
185
- public function getValidator ()
186
- {
187
- return $ this ->validator ;
188
- }
189
251
}
0 commit comments