Passed
Pull Request — master (#176)
by Wilmer
11:38
created

FormErrors::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
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 770
    public function __construct(array $attributesErrors = [])
16
    {
17
        /** @psalm-var array<string, array<array-key, string>> */
18 770
        $this->attributesErrors = $attributesErrors;
19 770
    }
20
21 29
    public function addError(string $attribute, string $error): void
22
    {
23 29
        $this->attributesErrors[$attribute][] = $error;
24 29
    }
25
26 8
    public function getAllErrors(): array
27
    {
28 8
        return $this->attributesErrors;
29
    }
30
31 2
    public function getErrors(string $attribute): array
32
    {
33 2
        return $this->attributesErrors[$attribute] ?? [];
34
    }
35
36 6
    public function getErrorSummary(array $onlyAttributes = []): array
37
    {
38 6
        $errors = $this->getAllErrors();
39
40 6
        if ($onlyAttributes !== []) {
41 3
            $errors = array_intersect_key($errors, array_flip($onlyAttributes));
42
        }
43
44 6
        return $this->renderErrorSummary($errors);
45
    }
46
47 8
    public function getErrorSummaryFirstErrors(): array
48
    {
49 8
        return $this->renderErrorSummary([$this->getFirstErrors()]);
50
    }
51
52 363
    public function getFirstError(string $attribute): string
53
    {
54 363
        if (empty($this->attributesErrors[$attribute])) {
55 349
            return '';
56
        }
57
58 15
        return reset($this->attributesErrors[$attribute]);
59
    }
60
61 9
    public function getFirstErrors(): array
62
    {
63 9
        if (empty($this->attributesErrors)) {
64 2
            return [];
65
        }
66
67 7
        $errors = [];
68
69 7
        foreach ($this->attributesErrors as $name => $es) {
70 7
            if (!empty($es)) {
71 7
                $errors[$name] = reset($es);
72
            }
73
        }
74
75 7
        return $errors;
76
    }
77
78 8
    public function hasErrors(?string $attribute = null): bool
79
    {
80 8
        return $attribute === null ? !empty($this->attributesErrors) : isset($this->attributesErrors[$attribute]);
81
    }
82
83
    public function clear(string $attribute = null): void
84
    {
85
        if ($attribute !== null) {
86
            unset($this->attributesErrors[$attribute]);
87
        } else {
88
            $this->attributesErrors = [];
89
        }
90
    }
91
92 10
    private function renderErrorSummary(array $errors): array
93
    {
94 10
        $lines = [];
95
96
        /** @var string[] errors */
97 10
        foreach ($errors as $error) {
98 10
            $lines = array_merge($lines, $error);
99
        }
100
101 10
        return $lines;
102
    }
103
}
104