Passed
Push — master ( 7bf385...40cf15 )
by Wilmer
57s queued 13s
created

FormErrors   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 92
ccs 37
cts 38
cp 0.9737
rs 10
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrors() 0 3 1
A addError() 0 3 1
A getAllErrors() 0 3 1
A __construct() 0 4 1
A getFirstErrors() 0 15 4
A renderErrorSummary() 0 10 2
A getFirstError() 0 7 2
A hasErrors() 0 3 2
A getErrorSummary() 0 9 2
A getErrorSummaryFirstErrors() 0 3 1
A clear() 0 6 2
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 376
    public function __construct(array $attributesErrors = [])
16
    {
17
        /** @psalm-var array<string, array<array-key, string>> */
18 376
        $this->attributesErrors = $attributesErrors;
19
    }
20
21 29
    public function addError(string $attribute, string $error): void
22
    {
23 29
        $this->attributesErrors[$attribute][] = $error;
24
    }
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 29
    public function clear(?string $attribute = null): void
84
    {
85 29
        if ($attribute !== null) {
86 29
            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