Test Failed
Pull Request — master (#364)
by Alexander
03:30 queued 45s
created

Result::getCommonErrorMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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