|
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; |
|
13
|
|
|
|
|
14
|
|
|
use RecursiveIteratorIterator; |
|
15
|
|
|
|
|
16
|
|
|
final class Validation |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Result |
|
20
|
|
|
*/ |
|
21
|
|
|
private $result; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Factory |
|
25
|
|
|
*/ |
|
26
|
|
|
private $factory; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $locale; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Initializes the object. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Result $result |
|
37
|
|
|
* @param Factory $factory |
|
38
|
|
|
* @param string $locale |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Result $result, Factory $factory, string $locale) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->result = $result; |
|
43
|
|
|
$this->factory = $factory; |
|
44
|
|
|
$this->locale = $locale; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function isValid(): bool |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->result->isValid(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getMessages(): array |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->createMessages($this->result); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getRootMessage(): Message |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->factory->message($this->result); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getMainMessage(): Message |
|
63
|
|
|
{ |
|
64
|
|
|
return \current($this->getMessages()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getFullMessage(): Message |
|
68
|
|
|
{ |
|
69
|
|
|
$messages = ['- '.$this->factory->message($this->result)->__toString()]; |
|
70
|
|
|
$iterator = new RecursiveIteratorIterator( |
|
71
|
|
|
new ResultRecursiveIterator($this->result), |
|
72
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
|
73
|
|
|
); |
|
74
|
|
|
$lastDepth = 0; |
|
75
|
|
|
$lastDepthOriginal = 0; |
|
76
|
|
|
$lastStringKey = null; |
|
77
|
|
|
$knownDepths = []; |
|
78
|
|
|
/* @var Result $childResult */ |
|
79
|
|
|
foreach ($iterator as $childKey => $childResult) { |
|
80
|
|
|
$message = $this->factory->message($childResult); |
|
81
|
|
|
$childrenCount = \count($iterator->callGetChildren()); |
|
82
|
|
|
|
|
83
|
|
|
if ($childrenCount == 1) { |
|
84
|
|
|
$lastStringKey = is_string($childKey) ? $childKey : null; |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$currentDepth = $lastDepth; |
|
89
|
|
|
$currentDepthOriginal = $iterator->getDepth() + 1; |
|
90
|
|
|
|
|
91
|
|
|
if (isset($knownDepths[$currentDepthOriginal])) { |
|
92
|
|
|
$currentDepth = $knownDepths[$currentDepthOriginal]; |
|
93
|
|
|
} elseif ($currentDepthOriginal > $lastDepthOriginal && $childrenCount != 1) { |
|
94
|
|
|
++$currentDepth; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (!isset($knownDepths[$currentDepthOriginal])) { |
|
98
|
|
|
$knownDepths[$currentDepthOriginal] = $currentDepth; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$lastDepth = $currentDepth; |
|
102
|
|
|
$lastDepthOriginal = $currentDepthOriginal; |
|
103
|
|
|
|
|
104
|
|
|
$placeholder = null; |
|
105
|
|
|
if ($lastDepth <= $currentDepth && is_string($lastStringKey)) { |
|
106
|
|
|
$placeholder = sprintf('"%s"', $lastStringKey); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$messages[] = \sprintf( |
|
110
|
|
|
'%s- %s', |
|
111
|
|
|
\str_repeat(' ', $currentDepth * 2), |
|
112
|
|
|
$message->render($placeholder) |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$lastStringKey = is_string($childKey) ? $childKey : null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return new Message('Validation', \implode(PHP_EOL, $messages), $this->result->getInput()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function createMessages(Result $result): array |
|
122
|
|
|
{ |
|
123
|
|
|
$key = \spl_object_hash($result); |
|
124
|
|
|
$message = $this->factory->message($result); |
|
125
|
|
|
$messages = [$key => $message]; |
|
126
|
|
|
foreach (new ResultIterator($result) as $childKey => $childResult) { |
|
127
|
|
|
if (!is_string($childKey)) { |
|
128
|
|
|
$childKey = \spl_object_hash($childResult); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$childMessage = $this->factory->message($childResult); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
if (!$childResult->hasChildren()) { |
|
134
|
|
|
$messages[$childKey] = $childMessage; |
|
135
|
|
|
continue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$messages[$childKey] = $this->createMessages($childResult); |
|
|
|
|
|
|
139
|
|
|
if (\count($messages[$childKey]) > 1) { |
|
140
|
|
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$messages[$childKey] = \current($messages[$childKey]); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if (\count($messages) > 1) { |
|
147
|
|
|
unset($messages[$key]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $messages; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: