Passed
Pull Request — master (#222)
by Rustam
13:44 queued 11:12
created

Result::__construct()   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 1
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
final class Result
8
{
9
    /**
10
     * @var Error[]
11
     */
12
    private array $errors = [];
13
    private ?string $attribute;
14
15 495
    public function __construct(string $attribute = null)
16
    {
17 495
        $this->attribute = $attribute;
18
    }
19
20 493
    public function isValid(): bool
21
    {
22 493
        return $this->errors === [];
23
    }
24
25 4
    public function isAttributeValid(string $attribute): bool
26
    {
27 4
        foreach ($this->errors as $error) {
28 4
            $firstItem = $error->getAttribute();
29 4
            if ($firstItem === $attribute) {
30 4
                return false;
31
            }
32
        }
33
34 4
        return true;
35
    }
36
37
    /**
38
     * @return Error[]
39
     */
40 494
    public function getErrors(): array
41
    {
42 494
        return $this->errors;
43
    }
44
45 269
    public function addError(string $message, array $parameters = [], string $attribute = null): self
46
    {
47 269
        if ($this->attribute !== null) {
48 8
            if ($attribute !== null) {
49 2
                $attribute = $this->attribute . '.' . $attribute;
50
            } else {
51 8
                $attribute = $this->attribute;
52
            }
53
        }
54 269
        $this->errors[] = new Error($message, $parameters, $attribute);
55
56 269
        return $this;
57
    }
58
59 16
    public function mergeError(Error $error): self
60
    {
61 16
        $this->addError(
62 16
            $error->getMessage(),
63 16
            $error->getParameters(),
64 16
            $error->getAttribute(),
65
        );
66
67 16
        return $this;
68
    }
69
}
70