Passed
Push — master ( 1b59d3...daf3c8 )
by Smoren
02:18
created

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