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 self |
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
|
|
|
/** |
126
|
|
|
* @return SplObjectStorage |
127
|
|
|
*/ |
128
|
|
|
public function getIterator() |
129
|
|
|
{ |
130
|
|
|
$childrenExceptions = new SplObjectStorage(); |
131
|
|
|
|
132
|
|
|
$recursiveIteratorIterator = $this->getRecursiveIterator(); |
133
|
|
|
$exceptionIterator = $recursiveIteratorIterator->getInnerIterator(); |
134
|
|
|
|
135
|
|
|
$lastDepth = 0; |
136
|
|
|
$lastDepthOriginal = 0; |
137
|
|
|
$knownDepths = []; |
138
|
|
|
foreach ($recursiveIteratorIterator as $childException) { |
139
|
|
|
if ($childException instanceof self |
140
|
|
|
&& $childException->getRelated()->count() > 0 |
141
|
|
|
&& $childException->getRelated()->count() < 2) { |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$currentDepth = $lastDepth; |
146
|
|
|
$currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1; |
147
|
|
|
|
148
|
|
|
if (isset($knownDepths[$currentDepthOriginal])) { |
149
|
|
|
$currentDepth = $knownDepths[$currentDepthOriginal]; |
150
|
|
|
} elseif ($currentDepthOriginal > $lastDepthOriginal |
151
|
|
|
&& ($this->hasCustomTemplate() || $exceptionIterator->count() != 1)) { |
152
|
|
|
++$currentDepth; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (!isset($knownDepths[$currentDepthOriginal])) { |
156
|
|
|
$knownDepths[$currentDepthOriginal] = $currentDepth; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$lastDepth = $currentDepth; |
160
|
|
|
$lastDepthOriginal = $currentDepthOriginal; |
161
|
|
|
|
162
|
|
|
$childrenExceptions->attach( |
163
|
|
|
$childException, |
164
|
|
|
[ |
165
|
|
|
'depth' => $currentDepth, |
166
|
|
|
'depth_original' => $currentDepthOriginal, |
167
|
|
|
'previous_depth' => $lastDepth, |
168
|
|
|
'previous_depth_original' => $lastDepthOriginal, |
169
|
|
|
] |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $childrenExceptions; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function getMessages() |
180
|
|
|
{ |
181
|
|
|
$messages = [$this->getMessage()]; |
182
|
|
|
foreach ($this as $exception) { |
183
|
|
|
$messages[] = $exception->getMessage(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (count($messages) > 1) { |
187
|
|
|
array_shift($messages); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $messages; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getFullMessage() |
197
|
|
|
{ |
198
|
|
|
$marker = '-'; |
199
|
|
|
$messages = []; |
200
|
|
|
$exceptions = $this->getIterator(); |
201
|
|
|
|
202
|
|
|
if ($this->hasCustomTemplate() || count($exceptions) != 1) { |
203
|
|
|
$messages[] = sprintf('%s %s', $marker, $this->getMessage()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
foreach ($exceptions as $exception) { |
207
|
|
|
$depth = $exceptions[$exception]['depth']; |
208
|
|
|
$prefix = str_repeat(' ', $depth * 2); |
209
|
|
|
$messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return implode(PHP_EOL, $messages); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return SplObjectStorage |
217
|
|
|
*/ |
218
|
3 |
|
public function getRelated() |
219
|
|
|
{ |
220
|
3 |
|
if (!$this->exceptions instanceof SplObjectStorage) { |
221
|
3 |
|
$this->exceptions = new SplObjectStorage(); |
222
|
3 |
|
} |
223
|
|
|
|
224
|
3 |
|
return $this->exceptions; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $name |
229
|
|
|
* @param mixed $value |
230
|
|
|
* |
231
|
|
|
* @return self |
232
|
|
|
*/ |
233
|
|
|
public function setParam($name, $value) |
234
|
|
|
{ |
235
|
|
|
if ('translator' === $name) { |
236
|
|
|
foreach ($this->getRelated() as $exception) { |
237
|
|
|
$exception->setParam($name, $value); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
parent::setParam($name, $value); |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return bool |
248
|
|
|
*/ |
249
|
1 |
|
private function isRelated($name, ValidationException $exception) |
250
|
|
|
{ |
251
|
1 |
|
return ($exception->getId() === $name || $exception->getName() === $name); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return ValidationException |
256
|
|
|
*/ |
257
|
1 |
|
public function getRelatedByName($name) |
258
|
|
|
{ |
259
|
1 |
|
if ($this->isRelated($name, $this)) { |
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
1 |
|
foreach ($this->getRecursiveIterator() as $exception) { |
264
|
1 |
|
if ($this->isRelated($name, $exception)) { |
265
|
1 |
|
return $exception; |
266
|
|
|
} |
267
|
1 |
|
} |
268
|
1 |
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param array $exceptions |
272
|
|
|
* |
273
|
|
|
* @return self |
274
|
|
|
*/ |
275
|
|
|
public function setRelated(array $exceptions) |
276
|
|
|
{ |
277
|
|
|
foreach ($exceptions as $exception) { |
278
|
|
|
$this->addRelated($exception); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|