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