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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Exceptions; |
15
|
|
|
|
16
|
|
|
use IteratorAggregate; |
17
|
|
|
use RecursiveIteratorIterator; |
18
|
|
|
use SplObjectStorage; |
19
|
|
|
use function count; |
20
|
|
|
use function is_array; |
21
|
|
|
|
22
|
|
|
class NestedValidationException extends ValidationException implements IteratorAggregate |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var SplObjectStorage |
26
|
|
|
*/ |
27
|
|
|
private $exceptions = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ValidationException $exception |
31
|
|
|
* |
32
|
|
|
* @return self |
33
|
|
|
*/ |
34
|
136 |
|
public function addRelated(ValidationException $exception) |
35
|
|
|
{ |
36
|
136 |
|
$this->getRelated()->attach($exception); |
37
|
|
|
|
38
|
136 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return RecursiveIteratorIterator |
43
|
|
|
*/ |
44
|
124 |
|
private function getRecursiveIterator() |
45
|
|
|
{ |
46
|
124 |
|
$exceptionIterator = new RecursiveExceptionIterator($this); |
47
|
124 |
|
$recursiveIteratorIterator = new RecursiveIteratorIterator( |
48
|
124 |
|
$exceptionIterator, |
49
|
124 |
|
RecursiveIteratorIterator::SELF_FIRST |
50
|
|
|
); |
51
|
|
|
|
52
|
124 |
|
return $recursiveIteratorIterator; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns weather an exception should be omitted or not. |
57
|
|
|
* |
58
|
|
|
* @param Exception $exception |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
123 |
|
private function isOmissible(Exception $exception) |
63
|
|
|
{ |
64
|
123 |
|
if (!$exception instanceof self) { |
65
|
115 |
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
13 |
|
$relatedExceptions = $exception->getRelated(); |
69
|
13 |
|
$relatedExceptions->rewind(); |
70
|
13 |
|
$childException = $relatedExceptions->current(); |
71
|
|
|
|
72
|
13 |
|
return 1 === $relatedExceptions->count() && !$childException instanceof NonOmissibleException; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return SplObjectStorage |
77
|
|
|
*/ |
78
|
124 |
|
public function getIterator() |
79
|
|
|
{ |
80
|
124 |
|
$childrenExceptions = new SplObjectStorage(); |
81
|
|
|
|
82
|
124 |
|
$recursiveIteratorIterator = $this->getRecursiveIterator(); |
83
|
124 |
|
$exceptionIterator = $recursiveIteratorIterator->getInnerIterator(); |
84
|
|
|
|
85
|
124 |
|
$lastDepth = 0; |
86
|
124 |
|
$lastDepthOriginal = 0; |
87
|
124 |
|
$knownDepths = []; |
88
|
124 |
|
foreach ($recursiveIteratorIterator as $childException) { |
89
|
123 |
|
if ($this->isOmissible($childException)) { |
90
|
5 |
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
123 |
|
$currentDepth = $lastDepth; |
94
|
123 |
|
$currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1; |
95
|
|
|
|
96
|
123 |
|
if (isset($knownDepths[$currentDepthOriginal])) { |
97
|
9 |
|
$currentDepth = $knownDepths[$currentDepthOriginal]; |
98
|
123 |
|
} elseif ($currentDepthOriginal > $lastDepthOriginal |
99
|
123 |
|
&& ($this->hasCustomTemplate() || 1 != $exceptionIterator->count())) { |
|
|
|
|
100
|
10 |
|
++$currentDepth; |
101
|
|
|
} |
102
|
|
|
|
103
|
123 |
|
if (!isset($knownDepths[$currentDepthOriginal])) { |
104
|
123 |
|
$knownDepths[$currentDepthOriginal] = $currentDepth; |
105
|
|
|
} |
106
|
|
|
|
107
|
123 |
|
$lastDepth = $currentDepth; |
108
|
123 |
|
$lastDepthOriginal = $currentDepthOriginal; |
109
|
|
|
|
110
|
123 |
|
$childrenExceptions->attach( |
111
|
123 |
|
$childException, |
112
|
|
|
[ |
113
|
123 |
|
'depth' => $currentDepth, |
114
|
123 |
|
'depth_original' => $currentDepthOriginal, |
115
|
123 |
|
'previous_depth' => $lastDepth, |
116
|
123 |
|
'previous_depth_original' => $lastDepthOriginal, |
117
|
|
|
] |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
124 |
|
return $childrenExceptions; |
122
|
|
|
} |
123
|
|
|
|
124
|
7 |
|
public function getMessages(array $templates = []): array |
125
|
|
|
{ |
126
|
7 |
|
$messages = [$this->getId() => $this->renderMessage($this, $templates)]; |
127
|
7 |
|
foreach ($this->getRelated() as $exception) { |
128
|
7 |
|
$id = $exception->getId(); |
129
|
7 |
|
if (!$exception instanceof self) { |
130
|
7 |
|
$messages[$id] = $this->renderMessage( |
131
|
7 |
|
$exception, |
132
|
7 |
|
$this->findTemplates($templates, $this->getId()) |
133
|
|
|
); |
134
|
7 |
|
continue; |
135
|
|
|
} |
136
|
|
|
|
137
|
4 |
|
$messages[$id] = $exception->getMessages($this->findTemplates($templates, $id, $this->getId())); |
138
|
4 |
|
if (count($messages[$id]) > 1) { |
|
|
|
|
139
|
2 |
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
$messages[$id] = current($messages[$exception->getId()]); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
if (count($messages) > 1) { |
146
|
7 |
|
unset($messages[$this->getId()]); |
147
|
|
|
} |
148
|
|
|
|
149
|
7 |
|
return $messages; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
124 |
|
public function getFullMessage() |
156
|
|
|
{ |
157
|
124 |
|
$marker = '-'; |
158
|
124 |
|
$messages = []; |
159
|
124 |
|
$exceptions = $this->getIterator(); |
160
|
|
|
|
161
|
124 |
|
if ($this->hasCustomTemplate() || 1 != count($exceptions)) { |
162
|
11 |
|
$messages[] = sprintf('%s %s', $marker, $this->getMessage()); |
163
|
|
|
} |
164
|
|
|
|
165
|
124 |
|
foreach ($exceptions as $exception) { |
166
|
123 |
|
$depth = $exceptions[$exception]['depth']; |
167
|
123 |
|
$prefix = str_repeat(' ', $depth * 2); |
168
|
123 |
|
$messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage()); |
169
|
|
|
} |
170
|
|
|
|
171
|
124 |
|
return implode(PHP_EOL, $messages); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return SplObjectStorage|ValidationException[] |
176
|
|
|
*/ |
177
|
137 |
|
public function getRelated() |
178
|
|
|
{ |
179
|
137 |
|
if (!$this->exceptions instanceof SplObjectStorage) { |
|
|
|
|
180
|
137 |
|
$this->exceptions = new SplObjectStorage(); |
181
|
|
|
} |
182
|
|
|
|
183
|
137 |
|
return $this->exceptions; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $exceptions |
188
|
|
|
* |
189
|
|
|
* @return self |
190
|
|
|
*/ |
191
|
134 |
|
public function setRelated(array $exceptions) |
192
|
|
|
{ |
193
|
134 |
|
foreach ($exceptions as $exception) { |
194
|
134 |
|
$this->addRelated($exception); |
195
|
|
|
} |
196
|
|
|
|
197
|
134 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
7 |
|
private function renderMessage(ValidationException $exception, array $templates): string |
201
|
|
|
{ |
202
|
7 |
|
if (isset($templates[$exception->getId()])) { |
203
|
2 |
|
$exception->updateTemplate($templates[$exception->getId()]); |
204
|
|
|
} |
205
|
|
|
|
206
|
7 |
|
return $exception->getMessage(); |
207
|
|
|
} |
208
|
|
|
|
209
|
7 |
|
private function findTemplates(array $templates, ...$ids): array |
210
|
|
|
{ |
211
|
7 |
|
while (count($ids) > 0) { |
212
|
7 |
|
$id = array_shift($ids); |
213
|
7 |
|
if (isset($templates[$id]) && is_array($templates[$id])) { |
214
|
1 |
|
$templates = $templates[$id]; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
7 |
|
return $templates; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|