Passed
Pull Request — master (#364)
by Alexander
05:15 queued 02:28
created

Result   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 44
c 1
b 0
f 0
dl 0
loc 142
ccs 54
cts 54
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrors() 0 3 1
A getErrorMessagesIndexedByPath() 0 9 2
A getErrorMessagesIndexedByAttribute() 0 13 3
A getErrorMessages() 0 3 1
A getCommonErrorMessages() 0 3 1
A getAttributeErrorMessages() 0 11 3
A addError() 0 5 1
A getAttributeErrorMessagesIndexedByPath() 0 14 3
A isValid() 0 3 1
A getAttributeErrors() 0 11 3
A isAttributeValid() 0 10 3
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 6
        return ArrayHelper::getColumn($this->errors, static fn (Error $error) => $error->getMessage());
52
    }
53
54
    /**
55
     * @psalm-return array<string, non-empty-list<string>>
56
     */
57 326
    public function getErrorMessagesIndexedByPath(string $separator = '.'): array
58
    {
59 326
        $errors = [];
60 326
        foreach ($this->errors as $error) {
61 325
            $stringValuePath = implode($separator, $error->getValuePath(true));
62 325
            $errors[$stringValuePath][] = $error->getMessage();
63
        }
64
65 326
        return $errors;
66
    }
67
68
    /**
69
     * @psalm-return array<string, non-empty-list<string>>
70
     *
71
     * @throws InvalidArgumentException
72
     */
73 16
    public function getErrorMessagesIndexedByAttribute(): array
74
    {
75 16
        $errors = [];
76 16
        foreach ($this->errors as $error) {
77 13
            $key = $error->getValuePath()[0] ?? '';
78 13
            if (!is_string($key)) {
79 1
                throw new InvalidArgumentException('Top level attributes can only have string type.');
80
            }
81
82 12
            $errors[$key][] = $error->getMessage();
83
        }
84
85 15
        return $errors;
86
    }
87
88
    /**
89
     * @return Error[]
90
     */
91 1
    public function getAttributeErrors(string $attribute): array
92
    {
93 1
        $errors = [];
94 1
        foreach ($this->errors as $error) {
95 1
            $firstItem = $error->getValuePath()[0] ?? '';
96 1
            if ($firstItem === $attribute) {
97 1
                $errors[] = $error;
98
            }
99
        }
100
101 1
        return $errors;
102
    }
103
104
    /**
105
     * @return string[]
106
     */
107 2
    public function getAttributeErrorMessages(string $attribute): array
108
    {
109 2
        $errors = [];
110 2
        foreach ($this->errors as $error) {
111 2
            $firstItem = $error->getValuePath()[0] ?? '';
112 2
            if ($firstItem === $attribute) {
113 2
                $errors[] = $error->getMessage();
114
            }
115
        }
116
117 2
        return $errors;
118
    }
119
120
    /**
121
     * @psalm-return array<string, non-empty-list<string>>
122
     */
123 1
    public function getAttributeErrorMessagesIndexedByPath(string $attribute, string $separator = '.'): array
124
    {
125 1
        $errors = [];
126 1
        foreach ($this->errors as $error) {
127 1
            $firstItem = $error->getValuePath()[0] ?? '';
128 1
            if ($firstItem !== $attribute) {
129 1
                continue;
130
            }
131
132 1
            $valuePath = implode($separator, array_slice($error->getValuePath(true), 1));
133 1
            $errors[$valuePath][] = $error->getMessage();
134
        }
135
136 1
        return $errors;
137
    }
138
139
    /**
140
     * @return string[]
141
     */
142 1
    public function getCommonErrorMessages(): array
143
    {
144 1
        return $this->getAttributeErrorMessages('');
145
    }
146
147
    /**
148
     * @psalm-param array<string,scalar|null> $parameters
149
     * @psalm-param list<int|string> $valuePath
150
     */
151 396
    public function addError(string $message, array $parameters = [], array $valuePath = []): self
152
    {
153 396
        $this->errors[] = new Error($message, $parameters, $valuePath);
154
155 396
        return $this;
156
    }
157
}
158