|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector; |
|
6
|
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
|
8
|
|
|
use Traversable; |
|
9
|
|
|
use Yiisoft\Validator\Result; |
|
10
|
|
|
|
|
11
|
|
|
final class ValidatorCollector implements ValidatorCollectorInterface, IndexCollectorInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use CollectorTrait; |
|
14
|
|
|
|
|
15
|
|
|
private array $validations = []; |
|
16
|
|
|
|
|
17
|
1 |
|
public function getCollected(): array |
|
18
|
|
|
{ |
|
19
|
1 |
|
return $this->validations; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
public function collect(mixed $value, iterable $rules, Result $result): void |
|
23
|
|
|
{ |
|
24
|
1 |
|
if (!$this->isActive()) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
$this->validations[] = [ |
|
29
|
|
|
'value' => $value, |
|
30
|
1 |
|
'rules' => $rules instanceof Traversable ? iterator_to_array($rules, true) : (array) $rules, |
|
31
|
1 |
|
'result' => $result->isValid(), |
|
32
|
1 |
|
'errors' => $result->getErrors(), |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
private function reset(): void |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
1 |
|
$this->validations = []; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
#[ArrayShape([ |
|
42
|
|
|
'validator.count' => 'int', |
|
43
|
|
|
'validator.count_valid' => 'int', |
|
44
|
|
|
'validator.count_invalid' => 'int', |
|
45
|
|
|
])] |
|
46
|
|
|
public function getIndexData(): array |
|
47
|
|
|
{ |
|
48
|
1 |
|
$count = count($this->validations); |
|
49
|
1 |
|
$countValid = count(array_filter($this->validations, fn (array $data) => $data['result'])); |
|
50
|
1 |
|
$countInvalid = $count - $countValid; |
|
51
|
|
|
|
|
52
|
|
|
return [ |
|
53
|
1 |
|
'validator.count' => $count, |
|
54
|
|
|
'validator.count_valid' => $countValid, |
|
55
|
|
|
'validator.count_invalid' => $countInvalid, |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.