Passed
Push — master ( 1d076d...54b566 )
by Alexander
02:18
created

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