Passed
Push — extract-errors ( ce0b18 )
by Dmitriy
03:11
created

FormErrors::getFirstErrors()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Yiisoft\Form;
5
6
class FormErrors
7
{
8
    private array $attributesErrors;
9
10 250
    public function __construct(array $attributesErrors = [])
11
    {
12 250
        $this->attributesErrors = $attributesErrors;
13 250
    }
14
15
    /**
16
     * Returns the errors for single attribute.
17
     *
18
     * @param string $attribute attribute name. Use null to retrieve errors for all attributes.
19
     *
20
     * @return array
21
     */
22 1
    public function getForAttribute(string $attribute): array
23
    {
24 1
        return $this->attributesErrors[$attribute] ?? [];
25
    }
26
27
28
    /**
29
     * Returns the errors for all attributes as a one-dimensional array.
30
     *
31
     * @param bool $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise
32
     * only the first error message for each attribute will be shown.
33
     *
34
     * @return array errors for all attributes as a one-dimensional array. Empty array is returned if no error.
35
     *
36
     * {@see getErrors()}
37
     * {@see getFirstErrors(){}
38
     */
39 4
    public function getErrorSummary(bool $showAllErrors): array
40
    {
41 4
        $lines = [];
42 4
        $errors = $showAllErrors ? $this->asArray() : [$this->getFirstErrors()];
43
44 4
        foreach ($errors as $error) {
45 4
            $lines = array_merge($lines, $error);
46
        }
47
48 4
        return $lines;
49
    }
50
51
    /**
52
     * Returns the first error of the specified attribute.
53
     *
54
     * @param string $attribute attribute name.
55
     *
56
     * @return string the error message. Empty string is returned if there is no error.
57
     *
58
     * {@see getErrors()}
59
     * {@see getFirstErrors()}
60
     */
61 68
    public function getFirstError(string $attribute): string
62
    {
63 68
        if (empty($this->attributesErrors[$attribute])) {
64 58
            return '';
65
        }
66
67 13
        return reset($this->attributesErrors[$attribute]);
68
    }
69
70
    /**
71
     * Returns the first error of every attribute in the model.
72
     *
73
     * @return array the first errors. The array keys are the attribute names, and the array values are the
74
     * corresponding error messages. An empty array will be returned if there is no error.
75
     *
76
     * {@see getErrors()}
77
     * {@see getFirstError()}
78
     */
79 3
    public function getFirstErrors(): array
80
    {
81 3
        if (empty($this->attributesErrors)) {
82 2
            return [];
83
        }
84
85 1
        $errors = [];
86
87 1
        foreach ($this->attributesErrors as $name => $es) {
88 1
            if (!empty($es)) {
89 1
                $errors[$name] = reset($es);
90
            }
91
        }
92
93 1
        return $errors;
94
    }
95
96
    /**
97
     * Returns a value indicating whether there is any validation error.
98
     *
99
     * @param string|null $attribute attribute name. Use null to check all attributes.
100
     *
101
     * @return bool whether there is any error.
102
     */
103 64
    public function hasErrors(?string $attribute = null): bool
104
    {
105 64
        return $attribute === null ? !empty($this->attributesErrors) : isset($this->attributesErrors[$attribute]);
106
    }
107
108
    /**
109
     * Add error for the specified attribute.
110
     *
111
     * @param string $attribute attribute name.
112
     * @param string $error attribute error message.
113
     */
114 20
    public function add(string $attribute, string $error): void
115
    {
116 20
        $this->attributesErrors[$attribute][] = $error;
117 20
    }
118
119 22
    public function clear(): void
120
    {
121 22
        $this->attributesErrors = [];
122 22
    }
123
124
    /**
125
     * Returns the errors for all attributes.
126
     *
127
     * @return array errors for all attributes or the specified attribute. null is returned if no error.
128
     *
129
     * Note that when returning errors for all attributes, the result is a two-dimensional array, like the following:
130
     *
131
     * ```php
132
     * [
133
     *     'username' => [
134
     *         'Username is required.',
135
     *         'Username must contain only word characters.',
136
     *     ],
137
     *     'email' => [
138
     *         'Email address is invalid.',
139
     *     ]
140
     * ]
141
     * ```
142
     *
143
     * {@see getFirstErrors()}
144
     * {@see getFirstError()}
145
     */
146 2
    public function asArray(): array
147
    {
148 2
        return $this->attributesErrors;
149
    }
150
}
151