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

ValidationResult   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 65
rs 10
c 2
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A ok() 0 3 1
A withErrors() 0 5 1
A of() 0 3 1
A addError() 0 5 1
A isOk() 0 3 1
A guard() 0 3 1
A getField() 0 3 1
A getErrors() 0 3 1
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