|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Respect\Validation\Exceptions; |
|
13
|
|
|
|
|
14
|
|
|
use IteratorAggregate; |
|
15
|
|
|
use RecursiveIteratorIterator; |
|
16
|
|
|
use SplObjectStorage; |
|
17
|
|
|
|
|
18
|
|
|
class NestedValidationException extends ValidationException implements IteratorAggregate |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var SplObjectStorage |
|
22
|
|
|
*/ |
|
23
|
|
|
private $exceptions = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param ValidationException $exception |
|
27
|
|
|
* |
|
28
|
|
|
* @return self |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function addRelated(ValidationException $exception) |
|
31
|
|
|
{ |
|
32
|
3 |
|
$this->getRelated()->attach($exception); |
|
33
|
|
|
|
|
34
|
3 |
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $path |
|
39
|
|
|
* @param ValidationException $exception |
|
40
|
|
|
* |
|
41
|
|
|
* @return ValidationException |
|
42
|
|
|
*/ |
|
43
|
|
|
private function getExceptionForPath($path, ValidationException $exception) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($path === $exception->guessId()) { |
|
46
|
|
|
return $exception; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!$exception instanceof self) { |
|
50
|
|
|
return $exception; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
foreach ($exception as $subException) { |
|
54
|
|
|
return $subException; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $exception; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $paths |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function findMessages(array $paths) |
|
66
|
|
|
{ |
|
67
|
|
|
$messages = []; |
|
68
|
|
|
|
|
69
|
|
|
foreach ($paths as $key => $value) { |
|
70
|
|
|
$numericKey = is_numeric($key); |
|
71
|
|
|
$path = $numericKey ? $value : $key; |
|
72
|
|
|
|
|
73
|
|
|
if (!($exception = $this->getRelatedByName($path))) { |
|
74
|
|
|
$exception = $this->findRelated($path); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$path = str_replace('.', '_', $path); |
|
78
|
|
|
|
|
79
|
|
|
if (!$exception) { |
|
80
|
|
|
$messages[$path] = ''; |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$exception = $this->getExceptionForPath($path, $exception); |
|
85
|
|
|
if (!$numericKey) { |
|
86
|
|
|
$exception->setTemplate($value); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$messages[$path] = $exception->getMainMessage(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $messages; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return Exception |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function findRelated($path) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$target = $this; |
|
101
|
1 |
|
$pieces = explode('.', $path); |
|
102
|
|
|
|
|
103
|
1 |
|
while (!empty($pieces) && $target) { |
|
104
|
1 |
|
$piece = array_shift($pieces); |
|
105
|
1 |
|
$target = $target->getRelatedByName($piece); |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $target; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return RecursiveIteratorIterator |
|
113
|
|
|
*/ |
|
114
|
1 |
|
private function getRecursiveIterator() |
|
115
|
|
|
{ |
|
116
|
1 |
|
$exceptionIterator = new RecursiveExceptionIterator($this); |
|
117
|
1 |
|
$recursiveIteratorIterator = new RecursiveIteratorIterator( |
|
118
|
1 |
|
$exceptionIterator, |
|
119
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
|
120
|
1 |
|
); |
|
121
|
|
|
|
|
122
|
1 |
|
return $recursiveIteratorIterator; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function isSkippable(ValidationException $exception) |
|
126
|
|
|
{ |
|
127
|
|
|
return $exception instanceof self |
|
128
|
|
|
&& 1 === $exception->getRelated()->count() |
|
129
|
|
|
&& false === $exception->hasCustomTemplate(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return SplObjectStorage |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getIterator() |
|
136
|
|
|
{ |
|
137
|
|
|
$childrenExceptions = new SplObjectStorage(); |
|
138
|
|
|
|
|
139
|
|
|
$recursiveIteratorIterator = $this->getRecursiveIterator(); |
|
140
|
|
|
$exceptionIterator = $recursiveIteratorIterator->getInnerIterator(); |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
$lastDepth = 0; |
|
143
|
|
|
$lastDepthOriginal = 0; |
|
144
|
|
|
$knownDepths = []; |
|
145
|
|
|
foreach ($recursiveIteratorIterator as $childException) { |
|
146
|
|
|
if ($this->isSkippable($childException)) { |
|
147
|
|
|
continue; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$currentDepth = $lastDepth; |
|
151
|
|
|
$currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1; |
|
152
|
|
|
|
|
153
|
|
|
if (isset($knownDepths[$currentDepthOriginal])) { |
|
154
|
|
|
$currentDepth = $knownDepths[$currentDepthOriginal]; |
|
155
|
|
|
} elseif ($currentDepthOriginal > $lastDepthOriginal) { |
|
156
|
|
|
++$currentDepth; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if (!isset($knownDepths[$currentDepthOriginal])) { |
|
160
|
|
|
$knownDepths[$currentDepthOriginal] = $currentDepth; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$lastDepth = $currentDepth; |
|
164
|
|
|
$lastDepthOriginal = $currentDepthOriginal; |
|
165
|
|
|
|
|
166
|
|
|
$childrenExceptions->attach( |
|
167
|
|
|
$childException, |
|
168
|
|
|
[ |
|
169
|
|
|
'depth' => $currentDepth, |
|
170
|
|
|
'depth_original' => $currentDepthOriginal, |
|
171
|
|
|
'previous_depth' => $lastDepth, |
|
172
|
|
|
'previous_depth_original' => $lastDepthOriginal, |
|
173
|
|
|
] |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $childrenExceptions; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getMessages() |
|
184
|
|
|
{ |
|
185
|
|
|
$messages = [$this->getMessage()]; |
|
186
|
|
|
foreach ($this as $exception) { |
|
187
|
|
|
$messages[] = $exception->getMessage(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if (count($messages) > 1) { |
|
191
|
|
|
array_shift($messages); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $messages; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getFullMessage() |
|
201
|
|
|
{ |
|
202
|
|
|
$messages = []; |
|
203
|
|
|
$leveler = 1; |
|
204
|
|
|
|
|
205
|
|
|
if (!$this->isSkippable($this)) { |
|
206
|
|
|
$leveler = 0; |
|
207
|
|
|
$messages[] = sprintf('- %s', $this->getMessage()); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$exceptions = $this->getIterator(); |
|
211
|
|
|
foreach ($exceptions as $exception) { |
|
212
|
|
|
$messages[] = sprintf( |
|
213
|
|
|
'%s- %s', |
|
214
|
|
|
str_repeat(' ', ($exceptions[$exception]['depth'] - $leveler) * 2), |
|
215
|
|
|
$exception->getMessage() |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return implode(PHP_EOL, $messages); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @return SplObjectStorage |
|
224
|
|
|
*/ |
|
225
|
3 |
|
public function getRelated() |
|
226
|
|
|
{ |
|
227
|
3 |
|
if (!$this->exceptions instanceof SplObjectStorage) { |
|
228
|
3 |
|
$this->exceptions = new SplObjectStorage(); |
|
229
|
3 |
|
} |
|
230
|
|
|
|
|
231
|
3 |
|
return $this->exceptions; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param string $name |
|
236
|
|
|
* @param mixed $value |
|
237
|
|
|
* |
|
238
|
|
|
* @return self |
|
239
|
|
|
*/ |
|
240
|
|
|
public function setParam($name, $value) |
|
241
|
|
|
{ |
|
242
|
|
|
if ('translator' === $name) { |
|
243
|
|
|
foreach ($this->getRelated() as $exception) { |
|
244
|
|
|
$exception->setParam($name, $value); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
parent::setParam($name, $value); |
|
249
|
|
|
|
|
250
|
|
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return bool |
|
255
|
|
|
*/ |
|
256
|
1 |
|
private function isRelated($name, ValidationException $exception) |
|
257
|
|
|
{ |
|
258
|
1 |
|
return ($exception->getId() === $name || $exception->getName() === $name); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @return ValidationException |
|
263
|
|
|
*/ |
|
264
|
1 |
|
public function getRelatedByName($name) |
|
265
|
|
|
{ |
|
266
|
1 |
|
if ($this->isRelated($name, $this)) { |
|
267
|
|
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
1 |
|
foreach ($this->getRecursiveIterator() as $exception) { |
|
271
|
1 |
|
if ($this->isRelated($name, $exception)) { |
|
272
|
1 |
|
return $exception; |
|
273
|
|
|
} |
|
274
|
1 |
|
} |
|
275
|
1 |
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param array $exceptions |
|
279
|
|
|
* |
|
280
|
|
|
* @return self |
|
281
|
|
|
*/ |
|
282
|
|
|
public function setRelated(array $exceptions) |
|
283
|
|
|
{ |
|
284
|
|
|
foreach ($exceptions as $exception) { |
|
285
|
|
|
$this->addRelated($exception); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
return $this; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.