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
|
|
|
public function __construct(Result $result, Factory $factory) |
29
|
|
|
{ |
30
|
|
|
$this->result = $result; |
31
|
|
|
$this->factory = $factory; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function isValid(): bool |
35
|
|
|
{ |
36
|
|
|
return $this->result->isValid(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function messages(Result $parentResult) |
40
|
|
|
{ |
41
|
|
|
$parentKey = spl_object_hash($parentResult); |
42
|
|
|
$parentMessage = $this->factory->message($parentResult); |
43
|
|
|
|
44
|
|
|
$messages = []; |
45
|
|
|
$messages[$parentKey] = $parentMessage->__toString(); |
46
|
|
|
|
47
|
|
|
foreach ($parentResult as $key => $result) { |
48
|
|
|
if (!is_string($key)) { |
49
|
|
|
$key = spl_object_hash($result); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$message = $this->factory->message($result); |
53
|
|
|
|
54
|
|
|
if (!$result->hasChildren()) { |
55
|
|
|
$messages[$key] = $message->__toString(); |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$messages[$key] = $this->messages($result); |
60
|
|
|
if (count($messages[$key]) == 1) { |
61
|
|
|
$messages[$key] = current($messages[$key]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($message->isIgnorable()) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($parentMessage->isIgnorable() && count($messages) > 1) { |
70
|
|
|
unset($messages[$parentKey]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $messages; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getMessage(): string |
77
|
|
|
{ |
78
|
|
|
return current($this->getMessages()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getMessages(): array |
82
|
|
|
{ |
83
|
|
|
return $this->messages($this->result); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|