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