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