Passed
Pull Request — master (#151)
by
unknown
03:25 queued 58s
created

Result   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 21
c 5
b 1
f 1
dl 0
loc 70
ccs 27
cts 27
cp 1
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 3 1
A getErrors() 0 5 1
A getErrorObjects() 0 3 1
A addError() 0 3 1
A getNestedDetailedErrors() 0 15 2
A getFlatDetailedErrors() 0 8 2
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 159
    public function isValid(): bool
17
    {
18 159
        return $this->errors === [];
19
    }
20
21
    /**
22
     * @psalm-param list<int|string>|null $valuePath
23
     */
24 129
    public function addError(string $message, array $valuePath = []): void
25
    {
26 129
        $this->errors[] = new Error($message, $valuePath);
27 129
    }
28
29
    /**
30
     * @return Error[]
31
     */
32 17
    public function getErrorObjects(): array
33
    {
34 17
        return $this->errors;
35
    }
36
37
    /**
38
     * @return string[]
39
     */
40 34
    public function getErrors(): array
41
    {
42 34
        return ArrayHelper::getColumn($this->errors, function ($error) {
43
            /** @var Error $error */
44 31
            return $error->getMessage();
45 34
        });
46
    }
47
48
    /**
49
     * @return array
50
     */
51 1
    public function getNestedDetailedErrors(): array
52
    {
53 1
        $valuePathCountMap = [];
54 1
        $errors = [];
55 1
        foreach ($this->errors as $error) {
56 1
            $stringValuePath = $error->getStringValuePath();
57 1
            $valuePathCount = $valuePathCountMap[$stringValuePath] ?? 0;
58 1
            $errorValuePath = "$stringValuePath.$valuePathCount";
59
60 1
            ArrayHelper::setValueByPath($errors, $errorValuePath, $error->getMessage());
61 1
            $valuePathCount++;
62 1
            $valuePathCountMap[$stringValuePath] = $valuePathCount;
63
        }
64
65 1
        return $errors;
66
    }
67
68
    /**
69
     * @return array
70
     */
71 1
    public function getFlatDetailedErrors(): array
72
    {
73 1
        $errors = [];
74 1
        foreach ($this->errors as $error) {
75 1
            $errors[$error->getStringValuePath()][] = $error->getMessage();
76
        }
77
78 1
        return $errors;
79
    }
80
}
81