1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
9
|
|
|
|
10
|
|
|
use function array_slice; |
11
|
|
|
use function implode; |
12
|
|
|
|
13
|
|
|
final class Result |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Error[] |
17
|
|
|
*/ |
18
|
|
|
private array $errors = []; |
19
|
|
|
|
20
|
162 |
|
public function isValid(): bool |
21
|
|
|
{ |
22
|
162 |
|
return $this->errors === []; |
23
|
|
|
} |
24
|
|
|
|
25
|
4 |
|
public function isAttributeValid(string $attribute): bool |
26
|
|
|
{ |
27
|
4 |
|
foreach ($this->errors as $error) { |
28
|
4 |
|
$firstItem = $error->getValuePath()[0] ?? ''; |
29
|
4 |
|
if ($firstItem === $attribute) { |
30
|
4 |
|
return false; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return Error[] |
39
|
|
|
*/ |
40
|
32 |
|
public function getErrorObjects(): array |
41
|
|
|
{ |
42
|
32 |
|
return $this->errors; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
30 |
|
public function getErrors(): array |
49
|
|
|
{ |
50
|
30 |
|
return ArrayHelper::getColumn($this->errors, static fn (Error $error) => $error->getMessage()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @psalm-return array<string, non-empty-list<string>> |
55
|
|
|
*/ |
56
|
3 |
|
public function getErrorsIndexedByPath(string $separator = '.'): array |
57
|
|
|
{ |
58
|
3 |
|
$errors = []; |
59
|
3 |
|
foreach ($this->errors as $error) { |
60
|
3 |
|
$stringValuePath = implode($separator, $error->getValuePath()); |
61
|
3 |
|
$errors[$stringValuePath][] = $error->getMessage(); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $errors; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @psalm-return array<int|string, non-empty-list<int|string>> |
69
|
|
|
*/ |
70
|
1 |
|
public function getErrorsIndexedByAttribute(): array |
71
|
|
|
{ |
72
|
1 |
|
$errors = []; |
73
|
1 |
|
foreach ($this->errors as $error) { |
74
|
1 |
|
$key = $error->getValuePath()[0] ?? ''; |
75
|
1 |
|
$errors[$key][] = $error->getMessage(); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return $errors; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Error[] |
83
|
|
|
*/ |
84
|
1 |
|
public function getAttributeErrorObjects(string $attribute): array |
85
|
|
|
{ |
86
|
1 |
|
return $this->getAttributeErrorsMap($attribute, static fn (Error $error): Error => $error); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string[] |
91
|
|
|
*/ |
92
|
5 |
|
public function getAttributeErrors(string $attribute): array |
93
|
|
|
{ |
94
|
5 |
|
return $this->getAttributeErrorsMap($attribute, static fn (Error $error): string => $error->getMessage()); |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
private function getAttributeErrorsMap(string $attribute, Closure $getErrorClosure): array |
98
|
|
|
{ |
99
|
6 |
|
$errors = []; |
100
|
6 |
|
foreach ($this->errors as $error) { |
101
|
6 |
|
$firstItem = $error->getValuePath()[0] ?? ''; |
102
|
6 |
|
if ($firstItem === $attribute) { |
103
|
6 |
|
$errors[] = $getErrorClosure($error); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
return $errors; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @psalm-return array<string, non-empty-list<string>> |
112
|
|
|
*/ |
113
|
1 |
|
public function getAttributeErrorsIndexedByPath(string $attribute, string $separator = '.'): array |
114
|
|
|
{ |
115
|
1 |
|
$errors = []; |
116
|
1 |
|
foreach ($this->errors as $error) { |
117
|
1 |
|
$firstItem = $error->getValuePath()[0] ?? ''; |
118
|
1 |
|
if ($firstItem !== $attribute) { |
119
|
1 |
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$valuePath = implode($separator, array_slice($error->getValuePath(), 1)); |
123
|
1 |
|
$errors[$valuePath][] = $error->getMessage(); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $errors; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string[] |
131
|
|
|
*/ |
132
|
1 |
|
public function getCommonErrors(): array |
133
|
|
|
{ |
134
|
1 |
|
return $this->getAttributeErrors(''); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @psalm-param array<int|string> $valuePath |
139
|
|
|
*/ |
140
|
144 |
|
public function addError(string $message, array $valuePath = []): void |
141
|
|
|
{ |
142
|
144 |
|
$this->errors[] = new Error($message, $valuePath); |
143
|
144 |
|
} |
144
|
|
|
} |
145
|
|
|
|