Passed
Pull Request — master (#132)
by Dmitriy
03:01
created

ValidatorInterfaceProxy::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 20
ccs 0
cts 9
cp 0
crap 12
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Proxy;
6
7
use Yiisoft\Validator\Result;
8
use Yiisoft\Validator\RulesProviderInterface;
9
use Yiisoft\Validator\ValidatorInterface;
10
use Yiisoft\Yii\Debug\Collector\ValidatorCollectorInterface;
11
12
final class ValidatorInterfaceProxy implements ValidatorInterface
13
{
14
    public function __construct(
15
        private ValidatorInterface $validator,
16
        private ValidatorCollectorInterface $collector,
17
    ) {
18
    }
19
20
    public function validate(mixed $data, iterable $rules = []): Result
21
    {
22
        $result = $this->validator->validate($data, $rules);
23
24
        if ($data instanceof RulesProviderInterface) {
25
            $explicitRules = $rules;
26
            $rules = (array) $data->getRules();
27
28
            foreach ($explicitRules as $key => $value) {
29
                $rules[$key] = $value;
30
            }
31
        }
32
33
        $this->collector->collect(
34
            $data,
35
            $rules,
36
            $result,
37
        );
38
39
        return $result;
40
    }
41
}
42