1 | <?php |
||
8 | class RuleFactory |
||
9 | { |
||
10 | /** |
||
11 | * Validator map allows for flexibility when creating a validation rule |
||
12 | * You can use 'required' instead of 'required' for the name of the rule |
||
13 | * or 'minLength'/'minlength' instead of 'MinLength' |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $validatorsMap = []; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $errorMessages = []; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $labeledErrorMessages = []; |
||
28 | |||
29 | /** |
||
30 | * Constructor |
||
31 | */ |
||
32 | 29 | public function __construct() |
|
33 | { |
||
34 | 29 | $this->registerDefaultRules(); |
|
35 | 29 | } |
|
36 | |||
37 | /** |
||
38 | * Set up the default rules that come with the library |
||
39 | */ |
||
40 | 29 | protected function registerDefaultRules() |
|
41 | { |
||
42 | $rulesClasses = [ |
||
43 | 29 | 'Alpha', |
|
44 | 'AlphaNumeric', |
||
45 | 'AlphaNumHyphen', |
||
46 | 'ArrayLength', |
||
47 | 'ArrayMaxLength', |
||
48 | 'ArrayMinLength', |
||
49 | 'Between', |
||
50 | 'Callback', |
||
51 | 'Date', |
||
52 | 'DateTime', |
||
53 | 'Email', |
||
54 | 'EmailDomain', |
||
55 | 'Equal', |
||
56 | 'FullName', |
||
57 | 'GreaterThan', |
||
58 | 'InList', |
||
59 | 'Integer', |
||
60 | 'IpAddress', |
||
61 | 'Length', |
||
62 | 'LessThan', |
||
63 | 'Match', |
||
64 | 'MaxLength', |
||
65 | 'MinLength', |
||
66 | 'NotEqual', |
||
67 | 'NotInList', |
||
68 | 'NotMatch', |
||
69 | 'NotRegex', |
||
70 | 'Number', |
||
71 | 'Regex', |
||
72 | 'Required', |
||
73 | 'RequiredWhen', |
||
74 | 'RequiredWith', |
||
75 | 'RequiredWithout', |
||
76 | 'Time', |
||
77 | 'Url', |
||
78 | 'Website', |
||
79 | 'File\Extension', |
||
80 | 'File\Image', |
||
81 | 'File\ImageHeight', |
||
82 | 'File\ImageRatio', |
||
83 | 'File\ImageWidth', |
||
84 | 'File\Size', |
||
85 | 'Upload\Required', |
||
86 | 'Upload\Extension', |
||
87 | 'Upload\Image', |
||
88 | 'Upload\ImageHeight', |
||
89 | 'Upload\ImageRatio', |
||
90 | 'Upload\ImageWidth', |
||
91 | 'Upload\Size', |
||
92 | ]; |
||
93 | 29 | foreach ($rulesClasses as $class) { |
|
94 | 29 | $fullClassName = '\\' . __NAMESPACE__ . '\Rule\\' . $class; |
|
95 | 29 | $name = strtolower(str_replace('\\', '', $class)); |
|
96 | 29 | $errorMessage = constant($fullClassName . '::MESSAGE'); |
|
97 | 29 | $labeledErrorMessage = constant($fullClassName . '::LABELED_MESSAGE'); |
|
98 | 29 | $this->register($name, $fullClassName, $errorMessage, $labeledErrorMessage); |
|
99 | } |
||
100 | 29 | } |
|
101 | |||
102 | |||
103 | /** |
||
104 | * Register a class to be used when creating validation rules |
||
105 | * |
||
106 | * @param string $name |
||
107 | * @param string $class |
||
108 | * |
||
109 | * @return \Sirius\Validation\RuleFactory |
||
110 | */ |
||
111 | 29 | public function register($name, $class, $errorMessage = '', $labeledErrorMessage = '') |
|
125 | |||
126 | /** |
||
127 | * Factory method to construct a validator based on options that are used most of the times |
||
128 | * |
||
129 | * @param string|callable $name |
||
130 | * name of a validator class or a callable object/function |
||
131 | * @param string|array $options |
||
132 | * validator options (an array, JSON string or QUERY string) |
||
133 | * @param string $messageTemplate |
||
134 | * error message template |
||
135 | * @param string $label |
||
136 | * label of the form input field or model attribute |
||
137 | * |
||
138 | * @throws \InvalidArgumentException |
||
139 | * @return \Sirius\Validation\Rule\AbstractValidator |
||
140 | */ |
||
141 | 26 | public function createRule($name, $options = null, $messageTemplate = null, $label = null) |
|
159 | |||
160 | /** |
||
161 | * Set default error message for a rule |
||
162 | * |
||
163 | * @param string $rule |
||
164 | * @param string|null $messageWithoutLabel |
||
165 | * @param string|null $messageWithLabel |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function setMessages($rule, $messageWithoutLabel = null, $messageWithLabel = null) |
||
180 | |||
181 | /** |
||
182 | * Get the error message saved in the registry for a rule, where the message |
||
183 | * is with or without a the label |
||
184 | * |
||
185 | * @param string $name name of the rule |
||
186 | * @param bool $withLabel |
||
187 | * |
||
188 | * @return string|NULL |
||
189 | */ |
||
190 | 21 | protected function getSuggestedMessageTemplate($name, $withLabel) |
|
201 | |||
202 | /** |
||
203 | * @param $name |
||
204 | * @param $options |
||
205 | * |
||
206 | * @return CallbackRule |
||
207 | */ |
||
208 | 26 | protected function construcRuleByNameAndOptions($name, $options) |
|
239 | } |
||
240 |