Passed
Pull Request — master (#170)
by Wilmer
02:26
created

FormErrors::addError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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