RespectValidator::inspect()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebTheory\Saveyour\Validation;
4
5
use Respect\Validation\Validatable;
6
use WebTheory\Saveyour\Contracts\Report\ValidationReportInterface;
7
use WebTheory\Saveyour\Contracts\Validation\ValidatorInterface;
8
use WebTheory\Saveyour\Report\Builder\ValidationReportBuilder;
9
10
class RespectValidator implements ValidatorInterface
11
{
12
    /**
13
     * @var array<int,Validatable>
14
     */
15
    protected array $validatables = [];
16
17 12
    public function __construct(Validatable ...$validatables)
18
    {
19 12
        $this->validatables = $validatables;
20
    }
21
22 12
    public function inspect($value): ValidationReportInterface
23
    {
24 12
        $builder = new ValidationReportBuilder();
25
26 12
        foreach ($this->validatables as $validatable) {
27 12
            if (!$validatable->validate($value)) {
28 6
                $builder->withRuleViolation($validatable->getName());
29 6
                $status = false;
30
            }
31
        }
32
33 12
        $builder->withValidationStatus($status ?? true);
34
35 12
        return $builder->build();
36
    }
37
38 6
    public function validate($value): bool
39
    {
40 6
        return $this->inspect($value)->validationStatus();
41
    }
42
}
43