1 | <?php |
||
26 | class Registry |
||
27 | { |
||
28 | /** |
||
29 | * @var array<string,callable> Associative array of definition name to function callback |
||
30 | */ |
||
31 | private $definitions = []; |
||
32 | /** |
||
33 | * @var \Caridea\Validate\Parser The parser |
||
34 | */ |
||
35 | private $parser; |
||
36 | |||
37 | /** |
||
38 | * @var array<string,callable> Associative array of definition name to function callback |
||
39 | */ |
||
40 | private static $defaultDefinitions = [ |
||
41 | 'required' => ['Caridea\Validate\Rule\Blank', 'required'], |
||
42 | 'not_empty' => ['Caridea\Validate\Rule\Blank', 'notEmpty'], |
||
43 | 'not_empty_list' => ['Caridea\Validate\Rule\Blank', 'notEmptyList'], |
||
44 | 'eq' => ['Caridea\Validate\Rule\Compare', 'eq'], |
||
45 | 'one_of' => ['Caridea\Validate\Rule\Compare', 'oneOf'], |
||
46 | 'min_length' => ['Caridea\Validate\Rule\Length', 'min'], |
||
47 | 'max_length' => ['Caridea\Validate\Rule\Length', 'max'], |
||
48 | 'length_equal' => ['Caridea\Validate\Rule\Length', 'equal'], |
||
49 | 'length_between' => ['Caridea\Validate\Rule\Length', 'between'], |
||
50 | 'like' => ['Caridea\Validate\Rule\Match', 'like'], |
||
51 | 'integer' => ['Caridea\Validate\Rule\Compare', 'integer'], |
||
52 | 'positive_integer' => ['Caridea\Validate\Rule\Compare', 'positiveInteger'], |
||
53 | 'decimal' => ['Caridea\Validate\Rule\Compare', 'decimal'], |
||
54 | 'positive_decimal' => ['Caridea\Validate\Rule\Compare', 'positiveDecimal'], |
||
55 | 'min_number' => ['Caridea\Validate\Rule\Compare', 'min'], |
||
56 | 'max_number' => ['Caridea\Validate\Rule\Compare', 'max'], |
||
57 | 'number_between' => ['Caridea\Validate\Rule\Compare', 'between'], |
||
58 | 'email' => ['Caridea\Validate\Rule\Match', 'email'], |
||
59 | 'iso_date' => ['Caridea\Validate\Rule\Match', 'isoDate'], |
||
60 | 'url' => ['Caridea\Validate\Rule\Match', 'url'], |
||
61 | 'timezone' => ['Caridea\Validate\Rule\Timezone', 'timezone'], |
||
62 | 'equal_to_field' => ['Caridea\Validate\Rule\Compare', 'equalToField'], |
||
63 | 'nested_object' => ['Caridea\Validate\Rule\Nested', 'nestedObject'], |
||
64 | 'list_of' => ['Caridea\Validate\Rule\Nested', 'listOf'], |
||
65 | 'list_of_objects' => ['Caridea\Validate\Rule\Nested', 'listOfObjects'], |
||
66 | 'list_of_different_objects' => ['Caridea\Validate\Rule\Nested', 'listOfDifferentObjects'], |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * Creates a new Validation rule registry. |
||
71 | */ |
||
72 | 4 | public function __construct() |
|
77 | |||
78 | /** |
||
79 | * Registers rule definitions. |
||
80 | * |
||
81 | * ```php |
||
82 | * $registry = new \Caridea\Validate\Registry(); |
||
83 | * $registry->register([ |
||
84 | * 'adult' => ['My\Validate\AgeRule', 'adult'], |
||
85 | * 'credit_card' => function(){return new CreditCardRule();}, |
||
86 | * 'something' => 'my_function_that_can_be_called' |
||
87 | * ]); |
||
88 | * ``` |
||
89 | * |
||
90 | * @param array<string,callable> $definitions Associative array of definition name to function callback |
||
91 | * @return $this provides a fluent interface |
||
92 | */ |
||
93 | 2 | public function register(array $definitions): self |
|
103 | |||
104 | /** |
||
105 | * Registers an alias for a ruleset. |
||
106 | * |
||
107 | * @param string $name The name of the alias |
||
108 | * @param object|array $rules The ruleset to alias |
||
109 | * @param string|null $error A custom error code to return, or `null` to use normal codes |
||
110 | * @return $this provides a fluent interface |
||
111 | */ |
||
112 | public function alias(string $name, $rules, ?string $error = null): self |
||
119 | |||
120 | /** |
||
121 | * Registers an alias for a ruleset, using a LIVR-compliant definition. |
||
122 | * |
||
123 | * ```javascript |
||
124 | * // alias.json |
||
125 | * { |
||
126 | * "name": "valid_address", |
||
127 | * "rules": { "nested_object": { |
||
128 | * "country": "required", |
||
129 | * "city": "required", |
||
130 | * "zip": "positive_integer" |
||
131 | * }}, |
||
132 | * error: "WRONG_ADDRESS" |
||
133 | * } |
||
134 | * ``` |
||
135 | * ```php |
||
136 | * $registry->aliasDefinition(json_decode(file_get_contents('alias.json'))); |
||
137 | * ``` |
||
138 | * |
||
139 | * @param array|object $definition The rule definition |
||
140 | * @return $this provides a fluent interface |
||
141 | * @throws \InvalidArgumentException if the definition is invalid |
||
142 | */ |
||
143 | 4 | public function aliasDefinition($definition): self |
|
156 | |||
157 | /** |
||
158 | * Constructs a validation rule. |
||
159 | * |
||
160 | * @param string $name A string name |
||
161 | * @param mixed $arg Optional constructor argument, or an array of arguments |
||
162 | * @return \Caridea\Validate\Rule The instantiated rule |
||
163 | * @throws \InvalidArgumentException if the rule name is not registered |
||
164 | * @throws \UnexpectedValueException if the factory returns a non-Rule |
||
165 | */ |
||
166 | 3 | public function factory(string $name, $arg = null): Rule |
|
179 | |||
180 | /** |
||
181 | * Creates a new Builder using this Repository. |
||
182 | * |
||
183 | * @return \Caridea\Validate\Builder The builder |
||
184 | */ |
||
185 | 1 | public function builder(): Builder |
|
189 | } |
||
190 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..