|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Validator\RuleInterface; |
|
9
|
|
|
use Yiisoft\Validator\DumpedRuleInterface; |
|
10
|
|
|
|
|
11
|
|
|
use function is_int; |
|
12
|
|
|
use function is_string; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* RulesDumper allows to get an array of rule names and corresponding settings from a set of rules. |
|
16
|
|
|
* The array is usually passed to the client to use it in client-side validation. |
|
17
|
|
|
* |
|
18
|
|
|
* @see RuleInterface |
|
19
|
|
|
* @see DumpedRuleInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
final class RulesDumper |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Return rules as array. |
|
25
|
|
|
* |
|
26
|
|
|
* For example: |
|
27
|
|
|
* |
|
28
|
|
|
* ```php |
|
29
|
|
|
* [ |
|
30
|
|
|
* 'name' => [ |
|
31
|
|
|
* [ |
|
32
|
|
|
* 'length', |
|
33
|
|
|
* 'min' => 4, |
|
34
|
|
|
* 'max' => 10, |
|
35
|
|
|
* 'exactly' => null, |
|
36
|
|
|
* 'lessThanMinMessage' => [ |
|
37
|
|
|
* 'template' => 'This value must contain at least {min, number} {min, plural, one{character} other{characters}}.', |
|
38
|
|
|
* 'parameters' => ['min' => 4], |
|
39
|
|
|
* ], |
|
40
|
|
|
* 'greaterThanMaxMessage' => [ |
|
41
|
|
|
* 'template' => 'This value must contain at most {max, number} {max, plural, one{character} other{characters}}.', |
|
42
|
|
|
* 'parameters' => ['max' => 10], |
|
43
|
|
|
* ], |
|
44
|
|
|
* 'notExactlyMessage' => [ |
|
45
|
|
|
* 'template' => 'This value must contain exactly {exactly, number} {exactly, plural, one{character} other{characters}}.', |
|
46
|
|
|
* 'parameters' => ['exactly' => null], |
|
47
|
|
|
* ], |
|
48
|
|
|
* 'incorrectInputMessage' => [ |
|
49
|
|
|
* 'template' => 'The value must be a string.', |
|
50
|
|
|
* 'parameters' => [], |
|
51
|
|
|
* ], |
|
52
|
|
|
* 'encoding' => 'UTF-8', |
|
53
|
|
|
* 'skipOnEmpty' => false, |
|
54
|
|
|
* 'skipOnError' => false, |
|
55
|
|
|
* ], |
|
56
|
|
|
* [ |
|
57
|
|
|
* 'callback', |
|
58
|
|
|
* ], |
|
59
|
|
|
* ], |
|
60
|
|
|
* // ... |
|
61
|
|
|
* ], |
|
62
|
|
|
* ``` |
|
63
|
|
|
* |
|
64
|
|
|
* @param iterable $rules Arrays of rule objects indexed by attributes. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array Array of rule names and corresponding settings indexed by attributes. |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function asArray(iterable $rules): array |
|
69
|
|
|
{ |
|
70
|
|
|
return self::fetchOptions($rules); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Converts rule objects to arrays of rule names and corresponding settings. |
|
75
|
|
|
* |
|
76
|
|
|
* @param iterable $rules Arrays of rule objects indexed by attributes. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array Array of rule names and corresponding settings indexed by attributes. |
|
79
|
|
|
*/ |
|
80
|
|
|
private static function fetchOptions(iterable $rules): array |
|
81
|
|
|
{ |
|
82
|
|
|
$result = []; |
|
83
|
|
|
/** |
|
84
|
|
|
* @var mixed $attribute |
|
85
|
|
|
* @var mixed $rule |
|
86
|
|
|
*/ |
|
87
|
|
|
foreach ($rules as $attribute => $rule) { |
|
88
|
|
|
if (!is_int($attribute) && !is_string($attribute)) { |
|
89
|
|
|
$message = sprintf( |
|
90
|
|
|
'An attribute can only have an integer or a string type. %s given.', |
|
91
|
|
|
get_debug_type($attribute), |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
throw new InvalidArgumentException($message); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (is_iterable($rule)) { |
|
98
|
|
|
$options = self::fetchOptions($rule); |
|
99
|
|
|
} elseif ($rule instanceof DumpedRuleInterface) { |
|
100
|
|
|
$options = array_merge([$rule->getName()], $rule->getOptions()); |
|
101
|
|
|
} elseif ($rule instanceof RuleInterface) { |
|
102
|
|
|
$options = [$rule::class]; |
|
103
|
|
|
} else { |
|
104
|
|
|
throw new InvalidArgumentException(sprintf( |
|
105
|
|
|
'Every rule must implement "%s". Type "%s" given.', |
|
106
|
|
|
RuleInterface::class, |
|
107
|
|
|
get_debug_type($rule), |
|
108
|
|
|
)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$result[$attribute] = $options; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|