Passed
Push — master ( bf8929...f56605 )
by Smoren
02:31
created

AnyOfCheck::__invoke()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 5
nop 3
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Checks;
6
7
use Smoren\Validator\Exceptions\CompositeCheckError;
8
use Smoren\Validator\Exceptions\ValidationError;
9
use Smoren\Validator\Interfaces\CheckInterface;
10
use Smoren\Validator\Structs\CheckName;
11
12
final class AnyOfCheck extends CompositeCheck implements CheckInterface
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17 7
    public function __invoke($value, array $previousErrors, bool $preventDuplicate = false): void
18
    {
19 7
        $errors = [];
20
21 7
        foreach ($this->rules as $rule) {
22
            try {
23 6
                $rule->validate($value);
24 3
                return;
25 6
            } catch (ValidationError $e) {
26 6
                $errors[] = $e;
27
            }
28
        }
29
30 4
        if (\count($errors) === 0) {
31 1
            return;
32
        }
33
34 3
        throw new CompositeCheckError(CheckName::ANY_OF, $value, $errors);
35
    }
36
}
37