RespectValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A inspect() 0 14 3
A __construct() 0 3 1
A validate() 0 3 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