Completed
Push — master ( 97e94f...81eace )
by Dmitriy
15s queued 13s
created

ValidatorCollector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 48
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollected() 0 3 1
A collect() 0 16 3
A getSummary() 0 11 1
A reset() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Debug;
6
7
use Traversable;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
10
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
11
12
final class ValidatorCollector implements SummaryCollectorInterface
13
{
14
    use CollectorTrait;
15
16
    private array $validations = [];
17
18
    public function getCollected(): array
19
    {
20
        return $this->validations;
21
    }
22
23
    public function collect(mixed $value, Result $result, callable|iterable|object|string|null $rules = null): void
24
    {
25
        if (!$this->isActive()) {
26
            return;
27
        }
28
29
        if ($rules instanceof Traversable) {
30
            $rules = iterator_to_array($rules);
31
        }
32
33
34
        $this->validations[] = [
35
            'value' => $value,
36
            'rules' => $rules,
37
            'result' => $result->isValid(),
38
            'errors' => $result->getErrors(),
39
        ];
40
    }
41
42
    public function getSummary(): array
43
    {
44
        $count = count($this->validations);
45
        $countValid = count(array_filter($this->validations, fn (array $data): bool => (bool)$data['result']));
46
        $countInvalid = $count - $countValid;
47
48
        return [
49
            'validator' => [
50
                'total' => $count,
51
                'valid' => $countValid,
52
                'invalid' => $countInvalid,
53
            ],
54
        ];
55
    }
56
57
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
58
    {
59
        $this->validations = [];
60
    }
61
}
62