Passed
Push — main ( 7afbf7...1813d6 )
by Breno
01:57
created

ErrorReporting::getStopSign()   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 0
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\GuardTrait;
7
use BrenoRoosevelt\Validation\Exception\ValidationExceptionFactoryInterface;
8
use BrenoRoosevelt\Validation\Exception\ValidationExceptionInterface;
9
10
class ErrorReporting implements Result
11
{
12
    use GuardTrait;
13
14
    /** @var Error[] */
15
    private array $errors = [];
16
    private int $stopSign = StopSign::DONT_STOP;
17
18
    final public function __construct(Error | Result ...$errors)
19
    {
20
        foreach ($errors as $errorOrResult) {
21
            array_push(
22
                $this->errors,
23
                ...($errorOrResult instanceof Result ? $errorOrResult->getErrors() : [$errorOrResult])
24
            );
25
        }
26
    }
27
28
    public static function success(): self
29
    {
30
        return new self;
31
    }
32
33
    public function getStopSign(): int
34
    {
35
        return $this->stopSign;
36
    }
37
38
    public function add(Error | Result ...$errors): self
39
    {
40
        return new self(...$this->errors, ...$errors);
41
    }
42
43
    public function addError(string $message, ?string $field = null, ?Rule $rule = null): self
44
    {
45
        return $this->add(new ErrorMessage($message, $field, $rule));
46
    }
47
48
    /** @inheritDoc */
49
    public function isOk(): bool
50
    {
51
        return empty($this->errors);
52
    }
53
54
    /** @inheritDoc */
55
    public function getErrors(): array
56
    {
57
        return $this->errors;
58
    }
59
60
    public function stopSign(): int
61
    {
62
        return $this->stopSign;
63
    }
64
65
    public function withStopSign(int $stopSign): self
66
    {
67
        $instance = clone $this;
68
        $instance->stopSign = $stopSign;
69
        return $instance;
70
    }
71
72
    /** @throws ValidationExceptionInterface */
73
    public function guard(
74
        ValidationExceptionFactoryInterface | ValidationExceptionInterface | string | null  $validationException = null
75
    ): void {
76
        $this->guardResult($this, $validationException);
77
    }
78
}
79