Passed
Pull Request — master (#151)
by
unknown
02:24
created

Result::getErrors()   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 Yiisoft\Arrays\ArrayHelper;
8
9
final class Result
10
{
11
    /**
12
     * @psalm-var array<int|string, string>
13
     */
14
    private array $errors = [];
15
16 159
    public function isValid(): bool
17
    {
18 159
        return $this->errors === [];
19
    }
20
21
    /**
22
     * Add an error.
23
     *
24
     * @param string $message Error message.
25
     * @param int|string|null $key For simple rules the key is null meaning error will be appended to the end of the
26
     * array. Otherwise, it's a path to a current error value in the input data concatenated using dot notation. For
27
     * example: "charts.0.points.0.coordinates.x".
28
     */
29 129
    public function addError(string $message, $key = null): void
30
    {
31 129
        if ($key !== null && $key !== 0 && !isset($this->errors[$key])) {
32 6
            $this->errors[$key] = $message;
33
        } else {
34 129
            $this->errors[] = $message;
35
        }
36 129
    }
37
38
    /**
39
     * @psalm-return array<int|string, string>
40
     */
41 37
    public function getErrors(): array
42
    {
43 37
        return $this->errors;
44
    }
45
46 1
    public function getNestedErrors(): array
47
    {
48 1
        $errors = [];
49 1
        foreach ($this->errors as $key => $message) {
50 1
            ArrayHelper::setValueByPath($errors, $key, $message);
51
        }
52
53 1
        return $errors;
54
    }
55
}
56