Passed
Push — master ( 72c1ff...1d076d )
by Alexander
02:24
created

Result::getErrorObjects()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
9
final class Result
10
{
11
    /**
12
     * @var Error[]
13
     */
14
    private array $errors = [];
15
16 169
    public function isValid(): bool
17
    {
18 169
        return $this->errors === [];
19
    }
20
21
    /**
22
     * @return Error[]
23
     */
24 24
    public function getErrorObjects(): array
25
    {
26 24
        return $this->errors;
27
    }
28
29
    /**
30
     * @return string[]
31
     */
32 38
    public function getErrors(): array
33
    {
34 38
        return ArrayHelper::getColumn($this->errors, static fn (Error $error) => $error->getMessage());
35
    }
36
37 4
    public function getErrorsIndexedByPath(string $separator = '.'): array
38
    {
39 4
        $errors = [];
40 4
        foreach ($this->errors as $error) {
41 4
            $stringValuePath = implode($separator, $error->getValuePath());
42 4
            $errors[$stringValuePath][] = $error->getMessage();
43
        }
44
45 4
        return $errors;
46
    }
47
48
    /**
49
     * @psalm-param array<int|string> $valuePath
50
     */
51 144
    public function addError(string $message, array $valuePath = []): void
52
    {
53 144
        $this->errors[] = new Error($message, $valuePath);
54 144
    }
55
}
56