Passed
Pull Request — master (#147)
by Wilmer
02:24
created

FormErrors::getFirstError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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