1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Helper; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use Yiisoft\Validator\RuleInterface; |
10
|
|
|
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider; |
11
|
|
|
use Yiisoft\Validator\RulesProviderInterface; |
12
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
13
|
|
|
|
14
|
|
|
use function is_callable; |
15
|
|
|
use function is_int; |
16
|
|
|
use function is_string; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @psalm-import-type RulesType from ValidatorInterface |
20
|
|
|
*/ |
21
|
|
|
final class RulesNormalizer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @psalm-param RulesType $rules |
25
|
|
|
* |
26
|
|
|
* @throws InvalidArgumentException |
27
|
|
|
* @throws ReflectionException |
28
|
|
|
* |
29
|
|
|
* @return iterable<int|string, iterable<int, RuleInterface>> |
30
|
824 |
|
*/ |
31
|
|
|
public static function normalize( |
32
|
|
|
callable|iterable|object|string|null $rules, |
33
|
|
|
mixed $data = null, |
34
|
|
|
?callable $defaultSkipOnEmptyCriteria = null, |
35
|
824 |
|
): iterable { |
36
|
|
|
$rules = self::prepareRulesArray($rules, $data); |
37
|
823 |
|
|
38
|
|
|
$normalizedRules = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var mixed $attribute |
42
|
|
|
* @var mixed $attributeRules |
43
|
823 |
|
*/ |
44
|
811 |
|
foreach ($rules as $attribute => $attributeRules) { |
45
|
1 |
|
if (!is_int($attribute) && !is_string($attribute)) { |
46
|
1 |
|
throw new InvalidArgumentException( |
47
|
|
|
sprintf( |
48
|
1 |
|
'An attribute can only have an integer or a string type. %s given.', |
49
|
|
|
get_debug_type($attribute), |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} |
53
|
810 |
|
|
54
|
810 |
|
$normalizedRules[$attribute] = new RulesNormalizerIterator( |
55
|
|
|
is_iterable($attributeRules) ? $attributeRules : [$attributeRules], |
56
|
|
|
$defaultSkipOnEmptyCriteria |
57
|
|
|
); |
58
|
|
|
} |
59
|
822 |
|
|
60
|
|
|
return $normalizedRules; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @throws InvalidArgumentException |
65
|
|
|
* |
66
|
|
|
* @return iterable<int, RuleInterface> |
67
|
824 |
|
*/ |
68
|
|
|
public static function normalizeList(iterable|callable|RuleInterface $rules): iterable |
69
|
|
|
{ |
70
|
|
|
return new RulesNormalizerIterator( |
71
|
824 |
|
is_iterable($rules) ? $rules : [$rules] |
|
|
|
|
72
|
34 |
|
); |
73
|
30 |
|
} |
74
|
34 |
|
|
75
|
|
|
/** |
76
|
|
|
* @psalm-param RulesType $rules |
77
|
794 |
|
* |
78
|
2 |
|
* @throws ReflectionException |
79
|
|
|
*/ |
80
|
|
|
private static function prepareRulesArray( |
81
|
792 |
|
callable|iterable|object|string|null $rules, |
82
|
1 |
|
mixed $data, |
83
|
|
|
): iterable { |
84
|
|
|
if ($rules === null) { |
85
|
|
|
return $data instanceof RulesProviderInterface |
86
|
791 |
|
? $data->getRules() |
87
|
790 |
|
: []; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
if ($rules instanceof RulesProviderInterface) { |
91
|
|
|
return $rules->getRules(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($rules instanceof RuleInterface || is_callable($rules)) { |
95
|
|
|
return [$rules]; |
96
|
|
|
} |
97
|
|
|
|
98
|
810 |
|
if (is_iterable($rules)) { |
99
|
|
|
return $rules; |
100
|
|
|
} |
101
|
810 |
|
|
102
|
810 |
|
return (new AttributesRulesProvider($rules))->getRules(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|