Passed
Push — main ( 70b219...5ef774 )
by Breno
02:19
created

ValidationResult::guard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
use BrenoRoosevelt\Validation\Exception\Guard;
7
use BrenoRoosevelt\Validation\Exception\ValidationExceptionInterface;
8
9
class ValidationResult implements Result
10
{
11
    use Guard;
12
13
    use BelongsToField {
14
        getField as private _getField;
15
    }
16
17
    /** @var string[] */
18
    private array $errors = [];
19
20
    public static function ok(): self
21
    {
22
        return new self;
23
    }
24
25
    public static function of(string $field, string ...$errors): self
26
    {
27
        return self::withErrors(...$errors)->field($field);
28
    }
29
30
    public static function withErrors(string ...$error): self
31
    {
32
        $instance = new self;
33
        $instance->errors = $error;
34
        return $instance;
35
    }
36
37
    public function addError(string ...$errors): self
38
    {
39
        $instance = clone $this;
40
        array_push($instance->errors, ...$errors);
41
        return $instance;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function isOk(): bool
48
    {
49
        return empty($this->errors);
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function getErrors(): array
56
    {
57
        return $this->errors;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getField(): ?string
64
    {
65
        return $this->_getField();
66
    }
67
68
    /**
69
     * @throws ValidationExceptionInterface
70
     */
71
    public function guard(?ValidationExceptionInterface $validationException = null): void
72
    {
73
        $this->guardResult($this, $validationException);
74
    }
75
}
76