Passed
Pull Request — master (#364)
by
unknown
02:34
created

Result::getAttributeErrorsMap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Closure;
8
use InvalidArgumentException;
9
use Yiisoft\Arrays\ArrayHelper;
10
11
use function array_slice;
12
use function implode;
13
use function is_string;
14
15
final class Result
16
{
17
    /**
18
     * @var Error[]
19
     */
20
    private array $errors = [];
21
22 669
    public function isValid(): bool
23
    {
24 669
        return $this->errors === [];
25
    }
26
27 4
    public function isAttributeValid(string $attribute): bool
28
    {
29 4
        foreach ($this->errors as $error) {
30 4
            $firstItem = $error->getValuePath()[0] ?? '';
31 4
            if ($firstItem === $attribute) {
32 4
                return false;
33
            }
34
        }
35
36 4
        return true;
37
    }
38
39
    /**
40
     * @return Error[]
41
     */
42 664
    public function getErrors(): array
43
    {
44 664
        return $this->errors;
45
    }
46
47
    /**
48
     * @return string[]
49
     */
50 6
    public function getErrorMessages(): array
51
    {
52 6
        return ArrayHelper::getColumn($this->errors, static fn (Error $error) => $error->getMessage());
53
    }
54
55
    /**
56
     * @psalm-return array<string, non-empty-list<string>>
57
     */
58 326
    public function getErrorMessagesIndexedByPath(string $separator = '.'): array
59
    {
60 326
        $errors = [];
61 326
        foreach ($this->errors as $error) {
62 325
            $stringValuePath = implode($separator, $error->getValuePath(true));
63 325
            $errors[$stringValuePath][] = $error->getMessage();
64
        }
65
66 326
        return $errors;
67
    }
68
69
    /**
70
     * @psalm-return array<string, non-empty-list<string>>
71
     *
72
     * @throws InvalidArgumentException
73
     */
74 16
    public function getErrorMessagesIndexedByAttribute(): array
75
    {
76 16
        $errors = [];
77 16
        foreach ($this->errors as $error) {
78 13
            $key = $error->getValuePath()[0] ?? '';
79 13
            if (!is_string($key)) {
80 1
                throw new InvalidArgumentException('Top level attributes can only have string type.');
81
            }
82
83 12
            $errors[$key][] = $error->getMessage();
84
        }
85
86 15
        return $errors;
87
    }
88
89
    /**
90
     * @return Error[]
91
     */
92 1
    public function getAttributeErrors(string $attribute): array
93
    {
94 1
        $errors = [];
95 1
        foreach ($this->errors as $error) {
96 1
            $firstItem = $error->getValuePath()[0] ?? '';
97 1
            if ($firstItem === $attribute) {
98 1
                $errors[] = $error;
99
            }
100
        }
101
102 1
        return $errors;
103
    }
104
105
    /**
106
     * @return string[]
107
     */
108 2
    public function getAttributeErrorMessages(string $attribute): array
109
    {
110 2
        $errors = [];
111 2
        foreach ($this->errors as $error) {
112 2
            $firstItem = $error->getValuePath()[0] ?? '';
113 2
            if ($firstItem === $attribute) {
114 2
                $errors[] = $error->getMessage();
115
            }
116
        }
117
118 2
        return $errors;
119
    }
120
121
    /**
122
     * @psalm-return array<string, non-empty-list<string>>
123
     */
124 1
    public function getAttributeErrorMessagesIndexedByPath(string $attribute, string $separator = '.'): array
125
    {
126 1
        $errors = [];
127 1
        foreach ($this->errors as $error) {
128 1
            $firstItem = $error->getValuePath()[0] ?? '';
129 1
            if ($firstItem !== $attribute) {
130 1
                continue;
131
            }
132
133 1
            $valuePath = implode($separator, array_slice($error->getValuePath(true), 1));
134 1
            $errors[$valuePath][] = $error->getMessage();
135
        }
136
137 1
        return $errors;
138
    }
139
140
    /**
141
     * @return string[]
142
     */
143 1
    public function getCommonErrorMessages(): array
144
    {
145 1
        return $this->getAttributeErrorMessages('');
146
    }
147
148
    /**
149
     * @psalm-param array<string,scalar|null> $parameters
150
     * @psalm-param list<int|string> $valuePath
151
     */
152 396
    public function addError(string $message, array $parameters = [], array $valuePath = []): self
153
    {
154 396
        $this->errors[] = new Error($message, $parameters, $valuePath);
155
156 396
        return $this;
157
    }
158
}
159