1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* FormErrors represents a form validation errors collection. |
9
|
|
|
*/ |
10
|
|
|
final class FormErrors implements FormErrorsInterface |
11
|
|
|
{ |
12
|
|
|
/** @psalm-var array<string, array<array-key, string>> */ |
13
|
|
|
private array $attributesErrors; |
14
|
|
|
|
15
|
738 |
|
public function __construct(array $attributesErrors = []) |
16
|
|
|
{ |
17
|
|
|
/** @psalm-var array<string, array<array-key, string>> */ |
18
|
738 |
|
$this->attributesErrors = $attributesErrors; |
19
|
738 |
|
} |
20
|
|
|
|
21
|
15 |
|
public function addError(string $attribute, string $error): void |
22
|
|
|
{ |
23
|
15 |
|
$this->attributesErrors[$attribute][] = $error; |
24
|
15 |
|
} |
25
|
|
|
|
26
|
4 |
|
public function getAllErrors(): array |
27
|
|
|
{ |
28
|
4 |
|
return $this->attributesErrors; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function getErrors(string $attribute): array |
32
|
|
|
{ |
33
|
1 |
|
return $this->attributesErrors[$attribute] ?? []; |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
public function getErrorSummary(): array |
37
|
|
|
{ |
38
|
3 |
|
return $this->renderErrorSumary($this->getAllErrors()); |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
public function getErrorSummaryFirstErrors(): array |
42
|
|
|
{ |
43
|
5 |
|
return $this->renderErrorSumary([$this->getFirstErrors()]); |
44
|
|
|
} |
45
|
|
|
|
46
|
340 |
|
public function getFirstError(string $attribute): string |
47
|
|
|
{ |
48
|
340 |
|
if (empty($this->attributesErrors[$attribute])) { |
49
|
333 |
|
return ''; |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
return reset($this->attributesErrors[$attribute]); |
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
public function getFirstErrors(): array |
56
|
|
|
{ |
57
|
6 |
|
if (empty($this->attributesErrors)) { |
58
|
2 |
|
return []; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
$errors = []; |
62
|
|
|
|
63
|
4 |
|
foreach ($this->attributesErrors as $name => $es) { |
64
|
4 |
|
if (!empty($es)) { |
65
|
4 |
|
$errors[$name] = reset($es); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
return $errors; |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
public function hasErrors(?string $attribute = null): bool |
73
|
|
|
{ |
74
|
5 |
|
return $attribute === null ? !empty($this->attributesErrors) : isset($this->attributesErrors[$attribute]); |
75
|
|
|
} |
76
|
|
|
|
77
|
15 |
|
public function clear(string $attribute = null): void |
78
|
|
|
{ |
79
|
15 |
|
if ($attribute !== null) { |
80
|
15 |
|
unset($this->attributesErrors[$attribute]); |
81
|
|
|
} else { |
82
|
|
|
$this->attributesErrors = []; |
83
|
|
|
} |
84
|
15 |
|
} |
85
|
|
|
|
86
|
6 |
|
private function renderErrorSumary(array $errors): array |
87
|
|
|
{ |
88
|
6 |
|
$lines = []; |
89
|
|
|
|
90
|
|
|
/** @var string[] errors */ |
91
|
6 |
|
foreach ($errors as $error) { |
92
|
6 |
|
$lines = array_merge($lines, $error); |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
return $lines; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|