RespectValidator::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 1
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