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

Result   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 45
ccs 14
cts 14
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrors() 0 3 1
A isValid() 0 3 1
A getErrorObjects() 0 3 1
A getErrorsIndexedByPath() 0 9 2
A addError() 0 3 1
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