|
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 getMainMessage(): Message |
|
58
|
|
|
{ |
|
59
|
|
|
return \current($this->getMessages()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getFullMessage(): Message |
|
63
|
|
|
{ |
|
64
|
|
|
$messages = [$this->factory->message($this->result)->__toString()]; |
|
65
|
|
|
$iterator = new RecursiveIteratorIterator( |
|
66
|
|
|
new ResultRecursiveIterator($this->result), |
|
67
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
|
68
|
|
|
); |
|
69
|
|
|
foreach ($iterator as $childKey => $childResult) { |
|
70
|
|
|
if ($iterator->callHasChildren() |
|
71
|
|
|
&& \count($iterator->callGetChildren()) > 1) { |
|
72
|
|
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
$messages[] = \sprintf( |
|
75
|
|
|
'%s- %s', |
|
76
|
|
|
\str_repeat(' ', $iterator->getDepth()), |
|
77
|
|
|
$this->factory->message($childResult)->__toString() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return new Message('Validation', \implode(PHP_EOL, $messages), $this->result->getInput()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function createMessages(Result $result): array |
|
85
|
|
|
{ |
|
86
|
|
|
$key = \spl_object_hash($result); |
|
87
|
|
|
$message = $this->factory->message($result); |
|
88
|
|
|
$messages = [$key => $message]; |
|
89
|
|
|
foreach (new ResultIterator($result) as $childKey => $childResult) { |
|
90
|
|
|
if (!is_string($childKey)) { |
|
91
|
|
|
$childKey = \spl_object_hash($childResult); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$childMessage = $this->factory->message($childResult); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
if (!$childResult->hasChildren()) { |
|
97
|
|
|
$messages[$childKey] = $childMessage; |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$messages[$childKey] = $this->createMessages($childResult); |
|
|
|
|
|
|
102
|
|
|
if (\count($messages[$childKey]) > 1) { |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$messages[$childKey] = \current($messages[$childKey]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (\count($messages) > 1) { |
|
110
|
|
|
unset($messages[$key]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $messages; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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: