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
|
|
|
} |
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
|
1 |
|
RecursiveIteratorIterator::SELF_FIRST |
120
|
|
|
); |
121
|
|
|
|
122
|
1 |
|
return $recursiveIteratorIterator; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function isSkippable(ValidationException $exception) |
126
|
|
|
{ |
127
|
|
|
if (!$exception instanceof self) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (1 !== $exception->getRelated()->count()) { |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!$exception->hasCustomTemplate()) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->hasChildTemplate($exception); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function hasChildTemplate(self $exception) |
143
|
|
|
{ |
144
|
|
|
$exception->getRelated()->rewind(); |
145
|
|
|
$childException = $exception->getRelated()->current(); |
146
|
|
|
|
147
|
|
|
return $childException->getMessage() === $exception->getMessage(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return SplObjectStorage |
152
|
|
|
*/ |
153
|
|
|
public function getIterator() |
154
|
|
|
{ |
155
|
|
|
$childrenExceptions = new SplObjectStorage(); |
156
|
|
|
|
157
|
|
|
$recursiveIteratorIterator = $this->getRecursiveIterator(); |
158
|
|
|
$exceptionIterator = $recursiveIteratorIterator->getInnerIterator(); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$lastDepth = 0; |
161
|
|
|
$lastDepthOriginal = 0; |
162
|
|
|
$knownDepths = []; |
163
|
|
|
foreach ($recursiveIteratorIterator as $childException) { |
164
|
|
|
if ($this->isSkippable($childException)) { |
165
|
|
|
continue; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$currentDepth = $lastDepth; |
169
|
|
|
$currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1; |
170
|
|
|
|
171
|
|
|
if (isset($knownDepths[$currentDepthOriginal])) { |
172
|
|
|
$currentDepth = $knownDepths[$currentDepthOriginal]; |
173
|
|
|
} elseif ($currentDepthOriginal > $lastDepthOriginal) { |
174
|
|
|
++$currentDepth; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (!isset($knownDepths[$currentDepthOriginal])) { |
178
|
|
|
$knownDepths[$currentDepthOriginal] = $currentDepth; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$lastDepth = $currentDepth; |
182
|
|
|
$lastDepthOriginal = $currentDepthOriginal; |
183
|
|
|
|
184
|
|
|
$childrenExceptions->attach( |
185
|
|
|
$childException, |
186
|
|
|
[ |
187
|
|
|
'depth' => $currentDepth, |
188
|
|
|
'depth_original' => $currentDepthOriginal, |
189
|
|
|
'previous_depth' => $lastDepth, |
190
|
|
|
'previous_depth_original' => $lastDepthOriginal, |
191
|
|
|
] |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $childrenExceptions; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public function getMessages() |
202
|
|
|
{ |
203
|
|
|
$messages = [$this->getMessage()]; |
204
|
|
|
foreach ($this as $exception) { |
205
|
|
|
$messages[] = $exception->getMessage(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (count($messages) > 1) { |
209
|
|
|
array_shift($messages); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $messages; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public function getFullMessage() |
219
|
|
|
{ |
220
|
|
|
$messages = []; |
221
|
|
|
$leveler = 1; |
222
|
|
|
|
223
|
|
|
if (!$this->isSkippable($this)) { |
224
|
|
|
$leveler = 0; |
225
|
|
|
$messages[] = sprintf('- %s', $this->getMessage()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$exceptions = $this->getIterator(); |
229
|
|
|
foreach ($exceptions as $exception) { |
230
|
|
|
$messages[] = sprintf( |
231
|
|
|
'%s- %s', |
232
|
|
|
str_repeat(' ', ($exceptions[$exception]['depth'] - $leveler) * 2), |
233
|
|
|
$exception->getMessage() |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return implode(PHP_EOL, $messages); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return SplObjectStorage |
242
|
|
|
*/ |
243
|
3 |
|
public function getRelated() |
244
|
|
|
{ |
245
|
3 |
|
if (!$this->exceptions instanceof SplObjectStorage) { |
246
|
3 |
|
$this->exceptions = new SplObjectStorage(); |
247
|
|
|
} |
248
|
|
|
|
249
|
3 |
|
return $this->exceptions; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $name |
254
|
|
|
* @param mixed $value |
255
|
|
|
* |
256
|
|
|
* @return self |
257
|
|
|
*/ |
258
|
|
|
public function setParam($name, $value) |
259
|
|
|
{ |
260
|
|
|
if ('translator' === $name) { |
261
|
|
|
foreach ($this->getRelated() as $exception) { |
262
|
|
|
$exception->setParam($name, $value); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
parent::setParam($name, $value); |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return bool |
273
|
|
|
*/ |
274
|
1 |
|
private function isRelated($name, ValidationException $exception) |
275
|
|
|
{ |
276
|
1 |
|
return ($exception->getId() === $name || $exception->getName() === $name); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return ValidationException |
281
|
|
|
*/ |
282
|
1 |
|
public function getRelatedByName($name) |
283
|
|
|
{ |
284
|
1 |
|
if ($this->isRelated($name, $this)) { |
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
1 |
|
foreach ($this->getRecursiveIterator() as $exception) { |
289
|
1 |
|
if ($this->isRelated($name, $exception)) { |
290
|
1 |
|
return $exception; |
291
|
|
|
} |
292
|
|
|
} |
293
|
1 |
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param array $exceptions |
297
|
|
|
* |
298
|
|
|
* @return self |
299
|
|
|
*/ |
300
|
|
|
public function setRelated(array $exceptions) |
301
|
|
|
{ |
302
|
|
|
foreach ($exceptions as $exception) { |
303
|
|
|
$this->addRelated($exception); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $this; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: