Passed
Push — main ( a3bcd5...954523 )
by Breno
01:33
created

AnyOf::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules;
5
6
use Attribute;
7
use BrenoRoosevelt\Validation\ErrorReporting;
8
use BrenoRoosevelt\Validation\Result;
9
use BrenoRoosevelt\Validation\Rule;
10
11
#[Attribute(Attribute::TARGET_PROPERTY)]
12
final class AnyOf implements Rule
13
{
14
    /** @var Rule[] */
15
    private array $rules;
16
17
    public function __construct(Rule ...$rules)
18
    {
19
        $this->rules = $rules;
20
    }
21
22
    public function validate(mixed $input, array $context = []): Result
23
    {
24
        $errorReporting = new ErrorReporting;
25
        foreach ($this->rules as $rule) {
26
            $result = $rule->validate($input, $context);
27
            if ($result->isOk()) {
28
                return ErrorReporting::success();
29
            }
30
31
            $errorReporting = $errorReporting->add($result);
32
        }
33
34
        return $errorReporting;
35
    }
36
}
37