Passed
Push — main ( f7f95e...c4cb1e )
by Breno
02:19
created

ErrorReporting   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 47
rs 10
c 1
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrors() 0 3 1
A __construct() 0 6 3
A isOk() 0 3 1
A addError() 0 3 1
A guard() 0 3 1
A add() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
use BrenoRoosevelt\Validation\Exception\GuardTrait;
7
use BrenoRoosevelt\Validation\Exception\ValidationExceptionInterface;
8
9
class ErrorReporting implements Result
10
{
11
    use GuardTrait;
12
13
    /** @var Error[] */
14
    private array $errors = [];
15
16
    final public function __construct(Error | Result ...$errors)
17
    {
18
        foreach ($errors as $errorOrResult) {
19
            array_push(
20
                $this->errors,
21
                ...($errorOrResult instanceof Error ? [$errorOrResult] : $errorOrResult->getErrors())
22
            );
23
        }
24
    }
25
26
    public function add(Error | Result ...$errors): self
27
    {
28
        return new self(...$this->errors, ...$errors);
29
    }
30
31
    public function addError(string $message, ?string $field = null): self
32
    {
33
        return $this->add(new ErrorMessage($message, $field));
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function isOk(): bool
40
    {
41
        return empty($this->errors);
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function getErrors(): array
48
    {
49
        return $this->errors;
50
    }
51
52
    /** @throws ValidationExceptionInterface */
53
    public function guard(ValidationExceptionInterface | string | null  $validationException = null): void
54
    {
55
        $this->guardResult($this, $validationException);
56
    }
57
}
58