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