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

Result   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 8
c 4
b 1
f 1
dl 0
loc 33
ccs 9
cts 9
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 3 1
A getErrors() 0 3 1
A addError() 0 6 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
final class Result
8
{
9
    /**
10
     * @psalm-var array<int|string, string>
11
     */
12
    private array $errors = [];
13
14 158
    public function isValid(): bool
15
    {
16 158
        return $this->errors === [];
17
    }
18
19
    /**
20
     * @param string $message
21
     * @param int|string|null $key For simple rules the key is null meaning error will be appended to the end of the
22
     * array. Otherwise, it's a path to a current error value in the input data concatenated using dot notation. For
23
     * example: "charts.0.points.0.coordinates.x".
24
     */
25 128
    public function addError(string $message, $key = null): void
26
    {
27 128
        if ($key !== null && $key !== 0) {
28 5
            $this->errors[$key] = $message;
29
        } else {
30 128
            $this->errors[] = $message;
31
        }
32 128
    }
33
34
    /**
35
     * @psalm-return array<int|string, string>
36
     */
37 36
    public function getErrors(): array
38
    {
39 36
        return $this->errors;
40
    }
41
}
42