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