Passed
Pull Request — master (#597)
by Dmitriy
16:40 queued 04:30
created

ValidatorCollector::getCollected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\CollectorInterface;
10
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
11
use Yiisoft\Yii\Debug\Collector\IndexCollectorInterface;
12
13
final class ValidatorCollector implements CollectorInterface, IndexCollectorInterface
14
{
15
    use CollectorTrait;
16
17
    private array $validations = [];
18
19
    public function getCollected(): array
20
    {
21
        return $this->validations;
22
    }
23
24
    public function collect(mixed $value, Result $result, ?iterable $rules = null): void
25
    {
26
        if (!$this->isActive()) {
27
            return;
28
        }
29
30
        $this->validations[] = [
31
            'value' => $value,
32
            'rules' => $rules instanceof Traversable ? iterator_to_array($rules, true) : (array) $rules,
33
            'result' => $result->isValid(),
34
            'errors' => $result->getErrors(),
35
        ];
36
    }
37
38
    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...
39
    {
40
        $this->validations = [];
41
    }
42
43
    public function getIndexData(): array
44
    {
45
        $count = count($this->validations);
46
        $countValid = count(array_filter($this->validations, fn (array $data) => $data['result']));
47
        $countInvalid = $count - $countValid;
48
49
        return [
50
            'validator' => [
51
                'total' => $count,
52
                'valid' => $countValid,
53
                'invalid' => $countInvalid,
54
            ],
55
        ];
56
    }
57
}
58