Passed
Push — master ( 39136d...c199ca )
by Smoren
02:15
created

AndRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 14
cp 0
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Rules;
6
7
use Smoren\Validator\Exceptions\ValidationError;
8
use Smoren\Validator\Interfaces\ValidationResultInterface;
9
use Smoren\Validator\Structs\ValidationSuccessResult;
10
11
class AndRule extends CompositeRule
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16
    protected function execute($value): ValidationResultInterface
17
    {
18
        $result = parent::execute($value);
19
        if ($result->preventNextChecks()) {
20
            return $result;
21
        }
22
23
        $result = new ValidationSuccessResult(false);
24
25
        $errors = [];
26
        foreach ($this->rules as $rule) {
27
            try {
28
                $rule->validate($value);
29
            } catch (ValidationError $e) {
30
                $errors[] = $e;
31
                break;
32
            }
33
        }
34
35
        if (\count($errors) === 0) {
36
            return $result;
37
        }
38
39
        throw ValidationError::fromValidationErrors($value, $errors);
40
    }
41
}
42