Passed
Push — master ( 32bdcd...b3a57a )
by Alexander
02:11
created

Result::getAttributeErrorObjects()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 163
    public function isValid(): bool
23
    {
24 163
        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 35
    public function getErrors(): array
43
    {
44 35
        return $this->errors;
45
    }
46
47
    /**
48
     * @return string[]
49
     */
50 23
    public function getErrorMessages(): array
51
    {
52 23
        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 3
    public function getErrorMessagesIndexedByPath(string $separator = '.'): array
59
    {
60 3
        $errors = [];
61 3
        foreach ($this->errors as $error) {
62 3
            $stringValuePath = implode($separator, $error->getValuePath());
63 3
            $errors[$stringValuePath][] = $error->getMessage();
64
        }
65
66 3
        return $errors;
67
    }
68
69
    /**
70
     * @psalm-return array<string, non-empty-list<string>>
71
     *
72
     * @throws InvalidArgumentException
73
     */
74 2
    public function getErrorMessagesIndexedByAttribute(): array
75
    {
76 2
        $errors = [];
77 2
        foreach ($this->errors as $error) {
78 2
            $key = $error->getValuePath()[0] ?? '';
79 2
            if (!is_string($key)) {
80 1
                throw new InvalidArgumentException('Top level attributes can only have string type.');
81
            }
82
83 1
            $errors[$key][] = $error->getMessage();
84
        }
85
86 1
        return $errors;
87
    }
88
89
    /**
90
     * @return Error[]
91
     */
92 4
    public function getAttributeErrors(string $attribute): array
93
    {
94 4
        return $this->getAttributeErrorsMap($attribute, static fn (Error $error): Error => $error);
95
    }
96
97
    /**
98
     * @return string[]
99
     */
100 2
    public function getAttributeErrorMessages(string $attribute): array
101
    {
102 2
        return $this->getAttributeErrorsMap($attribute, static fn (Error $error): string => $error->getMessage());
103
    }
104
105 6
    private function getAttributeErrorsMap(string $attribute, Closure $getErrorClosure): array
106
    {
107 6
        $errors = [];
108 6
        foreach ($this->errors as $error) {
109 6
            $firstItem = $error->getValuePath()[0] ?? '';
110 6
            if ($firstItem === $attribute) {
111 6
                $errors[] = $getErrorClosure($error);
112
            }
113
        }
114
115 6
        return $errors;
116
    }
117
118
    /**
119
     * @psalm-return array<string, non-empty-list<string>>
120
     */
121 1
    public function getAttributeErrorMessagesIndexedByPath(string $attribute, string $separator = '.'): array
122
    {
123 1
        $errors = [];
124 1
        foreach ($this->errors as $error) {
125 1
            $firstItem = $error->getValuePath()[0] ?? '';
126 1
            if ($firstItem !== $attribute) {
127 1
                continue;
128
            }
129
130 1
            $valuePath = implode($separator, array_slice($error->getValuePath(), 1));
131 1
            $errors[$valuePath][] = $error->getMessage();
132
        }
133
134 1
        return $errors;
135
    }
136
137
    /**
138
     * @return string[]
139
     */
140 1
    public function getCommonErrorMessages(): array
141
    {
142 1
        return $this->getAttributeErrorMessages('');
143
    }
144
145
    /**
146
     * @psalm-param array<int|string> $valuePath
147
     */
148 145
    public function addError(string $message, array $valuePath = []): void
149
    {
150 145
        $this->errors[] = new Error($message, $valuePath);
151 145
    }
152
}
153