1 | <?php |
||
10 | class RuleFactory |
||
11 | { |
||
12 | /** |
||
13 | * Validator map allows for flexibility when creating a validation rule |
||
14 | * You can use 'required' instead of 'required' for the name of the rule |
||
15 | * or 'minLength'/'minlength' instead of 'MinLength' |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $validatorsMap = []; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $errorMessages = []; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $labeledErrorMessages = []; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | */ |
||
34 | 29 | public function __construct() |
|
38 | |||
39 | /** |
||
40 | * Set up the default rules that come with the library |
||
41 | */ |
||
42 | 29 | protected function registerDefaultRules() |
|
103 | |||
104 | |||
105 | /** |
||
106 | * Register a class to be used when creating validation rules |
||
107 | * |
||
108 | * @param string $name |
||
109 | * @param string $class |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | 29 | public function register($name, $class, $errorMessage = '', $labeledErrorMessage = '') |
|
127 | |||
128 | /** |
||
129 | * Factory method to construct a validator based on options that are used most of the times |
||
130 | * |
||
131 | * @param string|callable $name |
||
132 | * name of a validator class or a callable object/function |
||
133 | * @param string|array $options |
||
134 | * validator options (an array, JSON string or QUERY string) |
||
135 | * @param string $messageTemplate |
||
136 | * error message template |
||
137 | * @param string $label |
||
138 | * label of the form input field or model attribute |
||
139 | * |
||
140 | * @throws \InvalidArgumentException |
||
141 | * @return AbstractRule |
||
142 | */ |
||
143 | 26 | public function createRule($name, $options = null, $messageTemplate = null, $label = null):AbstractRule |
|
161 | |||
162 | /** |
||
163 | * Set default error message for a rule |
||
164 | * |
||
165 | * @param string $rule |
||
166 | * @param string|null $messageWithoutLabel |
||
167 | * @param string|null $messageWithLabel |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function setMessages($rule, $messageWithoutLabel = null, $messageWithLabel = null) |
||
182 | |||
183 | /** |
||
184 | * Get the error message saved in the registry for a rule, where the message |
||
185 | * is with or without a the label |
||
186 | * |
||
187 | * @param string $name name of the rule |
||
188 | * @param bool $withLabel |
||
189 | * |
||
190 | * @return string|NULL |
||
191 | */ |
||
192 | 21 | protected function getSuggestedMessageTemplate($name, $withLabel) |
|
203 | |||
204 | /** |
||
205 | * @param $name |
||
206 | * @param $options |
||
207 | * |
||
208 | * @return CallbackRule |
||
209 | */ |
||
210 | 26 | protected function construcRuleByNameAndOptions($name, $options) |
|
241 | } |
||
242 |