Passed
Push — master ( c202e1...34af4b )
by Alexander
20:59 queued 18:00
created

FormErrors::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

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