|
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|ValidationException[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $exceptions = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return SplObjectStorage|ValidationException[] |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function getRelated(): SplObjectStorage |
|
34
|
|
|
{ |
|
35
|
2 |
|
if (!$this->exceptions instanceof SplObjectStorage) { |
|
36
|
2 |
|
$this->exceptions = new SplObjectStorage(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
return $this->exceptions; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $exceptions |
|
44
|
|
|
* |
|
45
|
|
|
* @return self |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setRelated(array $exceptions): self |
|
48
|
|
|
{ |
|
49
|
|
|
foreach ($exceptions as $exception) { |
|
50
|
|
|
$this->addRelated($exception); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param ValidationException $exception |
|
58
|
|
|
* |
|
59
|
|
|
* @return self |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function addRelated(ValidationException $exception) |
|
62
|
|
|
{ |
|
63
|
2 |
|
$this->getRelated()->attach($exception); |
|
64
|
|
|
|
|
65
|
2 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return SplObjectStorage|ValidationException[] |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getIterator(): SplObjectStorage |
|
72
|
|
|
{ |
|
73
|
|
|
$childrenExceptions = new SplObjectStorage(); |
|
74
|
|
|
|
|
75
|
|
|
$recursiveIteratorIterator = new RecursiveIteratorIterator( |
|
76
|
|
|
new RecursiveExceptionIterator($this), |
|
77
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
|
78
|
|
|
); |
|
79
|
|
|
$innerIterator = $recursiveIteratorIterator->getInnerIterator(); |
|
80
|
|
|
$iterator = new OmissibleFilterIterator($recursiveIteratorIterator); |
|
81
|
|
|
|
|
82
|
|
|
$currentDepth = 0; |
|
83
|
|
|
$originalLastDepth = 0; |
|
84
|
|
|
$knownDepths = []; |
|
85
|
|
|
foreach ($iterator as $childException) { |
|
86
|
|
|
$originalCurrentDepth = $recursiveIteratorIterator->getDepth() + 1; |
|
87
|
|
|
if (isset($knownDepths[$originalCurrentDepth])) { |
|
88
|
|
|
$currentDepth = $knownDepths[$originalCurrentDepth]; |
|
89
|
|
|
} elseif ($originalCurrentDepth > $originalLastDepth |
|
90
|
|
|
&& ($this->hasCustomTemplate() || 1 !== $innerIterator->count())) { |
|
|
|
|
|
|
91
|
|
|
$knownDepths[$originalCurrentDepth] = ++$currentDepth; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$childrenExceptions->attach($childException, $currentDepth); |
|
95
|
|
|
|
|
96
|
|
|
$originalLastDepth = $originalCurrentDepth; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $childrenExceptions; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getMessages(array $templates = []): array |
|
103
|
|
|
{ |
|
104
|
|
|
$messages = [$this->getId() => $this->renderMessage($this, $templates)]; |
|
105
|
|
|
foreach ($this->getRelated() as $exception) { |
|
106
|
|
|
$id = $exception->getId(); |
|
107
|
|
|
if (!$exception instanceof self) { |
|
108
|
|
|
$messages[$id] = $this->renderMessage( |
|
109
|
|
|
$exception, |
|
110
|
|
|
$this->findTemplates($templates, $this->getId()) |
|
111
|
|
|
); |
|
112
|
|
|
continue; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$messages[$id] = $exception->getMessages($this->findTemplates($templates, $id, $this->getId())); |
|
116
|
|
|
if (count($messages[$id]) > 1) { |
|
|
|
|
|
|
117
|
|
|
continue; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$messages[$id] = current($messages[$exception->getId()]); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (count($messages) > 1) { |
|
124
|
|
|
unset($messages[$this->getId()]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $messages; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getFullMessage(): string |
|
134
|
|
|
{ |
|
135
|
|
|
$marker = '-'; |
|
136
|
|
|
$messages = []; |
|
137
|
|
|
$exceptions = $this->getIterator(); |
|
138
|
|
|
|
|
139
|
|
|
if ($this->hasCustomTemplate() || 1 != count($exceptions)) { |
|
140
|
|
|
$messages[] = sprintf('%s %s', $marker, $this->getMessage()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
foreach ($exceptions as $exception) { |
|
144
|
|
|
$prefix = str_repeat(' ', $exceptions[$exception] * 2); |
|
145
|
|
|
$messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return implode(PHP_EOL, $messages); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
private function getRecursiveIterator(): RecursiveIteratorIterator |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
|
|
$exceptionIterator = new RecursiveExceptionIterator($this); |
|
154
|
|
|
$recursiveIteratorIterator = new RecursiveIteratorIterator( |
|
155
|
|
|
$exceptionIterator, |
|
156
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
return $recursiveIteratorIterator; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private function renderMessage(ValidationException $exception, array $templates): string |
|
163
|
|
|
{ |
|
164
|
|
|
if (isset($templates[$exception->getId()])) { |
|
165
|
|
|
$exception->updateTemplate($templates[$exception->getId()]); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $exception->getMessage(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
private function findTemplates(array $templates, ...$ids): array |
|
172
|
|
|
{ |
|
173
|
|
|
while (count($ids) > 0) { |
|
174
|
|
|
$id = array_shift($ids); |
|
175
|
|
|
if (isset($templates[$id]) && is_array($templates[$id])) { |
|
176
|
|
|
$templates = $templates[$id]; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $templates; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: