Test Failed
Pull Request — master (#163)
by
unknown
02:20
created

Result::getAttributeErrorsIndexedByPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
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 Yiisoft\Arrays\ArrayHelper;
9
10
final class Result
11
{
12
    /**
13
     * @var Error[]
14
     */
15
    private array $errors = [];
16 169
17
    public function isValid(): bool
18 169
    {
19
        return $this->errors === [];
20
    }
21
22
    public function isAttributeValid(string $attribute): bool
23
    {
24 24
        foreach ($this->errors as $error) {
25
            $firstItem = $error->getValuePath()[0] ?? null;
26 24
            if ($firstItem === $attribute) {
27
                return true;
28
            }
29
        }
30
31
        return false;
32 38
    }
33
34 38
    /**
35
     * @return Error[]
36
     */
37 4
    public function getErrorObjects(): array
38
    {
39 4
        return $this->errors;
40 4
    }
41 4
42 4
    /**
43
     * @return string[]
44
     */
45 4
    public function getErrors(): array
46
    {
47
        return ArrayHelper::getColumn($this->errors, static fn (Error $error) => $error->getMessage());
48
    }
49
50
    /**
51 144
     * @psalm-return array<string, non-empty-list<string>>
52
     */
53 144
    public function getErrorsIndexedByPath(string $separator = '.'): array
54 144
    {
55
        $errors = [];
56
        foreach ($this->errors as $error) {
57
            $stringValuePath = implode($separator, $error->getValuePath());
58
            $errors[$stringValuePath][] = $error->getMessage();
59
        }
60
61
        return $errors;
62
    }
63
64
    /**
65
     * @psalm-return array<string, Error[]>
66
     */
67
    public function getAttributeErrorObjects(string $attribute): array
68
    {
69
        return $this->getAttributeErrorsMap($attribute, static fn (Error $error): Error => $error);
70
    }
71
72
    /**
73
     * @psalm-return array<string, string[]>
74
     */
75
    public function getAttributeErrors(string $attribute): array
76
    {
77
        return $this->getAttributeErrorsMap($attribute, static fn (Error $error): string => $error->getMessage());
78
    }
79
80
    private function getAttributeErrorsMap(string $attribute, Closure $getErrorClosure): array
81
    {
82
        $errors = [];
83
        foreach ($this->errors as $error) {
84
            $firstItem = $error->getValuePath()[0] ?? null;
85
            if ($firstItem === $attribute) {
86
                $errors[] = $getErrorClosure($error);
87
            }
88
        }
89
90
        return $errors;
91
    }
92
93
    /**
94
     * @psalm-return list<string>
95
     */
96
    public function getAttributeErrorsIndexedByPath(string $attribute, string $separator = '.'): array
97
    {
98
        return $this->getErrorsIndexedByPath($separator)[$attribute] ?? [];
99
    }
100
101
    /**
102
     * @psalm-param array<int|string> $valuePath
103
     */
104
    public function addError(string $message, array $valuePath = []): void
105
    {
106
        $this->errors[] = new Error($message, $valuePath);
107
    }
108
}
109