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

Result::getAttributeErrorMessagesIndexedByPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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