Total Complexity | 41 |
Total Lines | 300 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Rules often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Rules, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Rules |
||
27 | { |
||
28 | use ErrorCollectorTrait; |
||
29 | |||
30 | /** |
||
31 | * Rules Clauses |
||
32 | * |
||
33 | * @access protected |
||
34 | * @type array |
||
35 | */ |
||
36 | protected $clauses = []; |
||
37 | |||
38 | /** |
||
39 | * Rules Custom Error Messages |
||
40 | * |
||
41 | * @access protected |
||
42 | * @type array |
||
43 | */ |
||
44 | protected $customErrors = []; |
||
45 | |||
46 | /** |
||
47 | * Source Variables |
||
48 | * |
||
49 | * @access protected |
||
50 | * @type array |
||
51 | */ |
||
52 | protected $sourceVars = []; |
||
53 | |||
54 | // ------------------------------------------------------------------------ |
||
55 | |||
56 | /** |
||
57 | * Rules::__construct |
||
58 | * |
||
59 | * @param array $sourceVars |
||
60 | */ |
||
61 | public function __construct($sourceVars = []) |
||
62 | { |
||
63 | language() |
||
64 | ->addFilePath(__DIR__ . DIRECTORY_SEPARATOR) |
||
65 | ->loadFile('rules') |
||
66 | ->loadFile('validation'); |
||
67 | |||
68 | $this->customErrors = [ |
||
69 | 'required' => language()->getLine('SECURITY_RULES_E_REQUIRED'), |
||
70 | 'float' => language()->getLine('SECURITY_RULES_E_FLOAT'), |
||
71 | 'email' => language()->getLine('SECURITY_RULES_E_EMAIL'), |
||
72 | 'integer' => language()->getLine('SECURITY_RULES_E_INTEGER'), |
||
73 | 'minLength' => language()->getLine('SECURITY_RULES_E_MINLENGTH'), |
||
74 | 'maxLength' => language()->getLine('SECURITY_RULES_E_MAXLENGTH'), |
||
75 | 'listed' => language()->getLine('SECURITY_RULES_E_LISTED'), |
||
76 | ]; |
||
77 | |||
78 | if ( ! empty($sourceVars)) { |
||
79 | if ($sourceVars instanceof \ArrayObject) { |
||
|
|||
80 | $sourceVars = $sourceVars->getArrayCopy(); |
||
81 | } |
||
82 | |||
83 | $this->sourceVars = $sourceVars; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Rules::setSource |
||
89 | * |
||
90 | * @param array $sourceVars |
||
91 | */ |
||
92 | public function setSource(array $sourceVars) |
||
93 | { |
||
94 | $this->sourceVars = $sourceVars; |
||
95 | } |
||
96 | |||
97 | // -------------------------------------------------------------------------------------- |
||
98 | |||
99 | /** |
||
100 | * Rules::addSource |
||
101 | * |
||
102 | * @param string $key |
||
103 | * @param string $value |
||
104 | */ |
||
105 | public function addSource($key, $value) |
||
106 | { |
||
107 | $this->sourceVars[ $key ] = $value; |
||
108 | } |
||
109 | |||
110 | // -------------------------------------------------------------------- |
||
111 | |||
112 | /** |
||
113 | * Rules::sets |
||
114 | * |
||
115 | * @param array $rules |
||
116 | */ |
||
117 | public function sets(array $rules) |
||
118 | { |
||
119 | foreach ($rules as $rule) { |
||
120 | $this->add($rule[ 'field' ], $rule[ 'label' ], $rule[ 'rules' ], $rule[ 'messages' ]); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | // -------------------------------------------------------------------- |
||
125 | |||
126 | /** |
||
127 | * Rules::add |
||
128 | * |
||
129 | * @param string $field |
||
130 | * @param string $label |
||
131 | * @param string $rules |
||
132 | * @param array $messages |
||
133 | */ |
||
134 | public function add($field, $label, $rules, $messages = []) |
||
135 | { |
||
136 | $this->clauses[ $field ] = [ |
||
137 | 'field' => $field, |
||
138 | 'label' => $label, |
||
139 | 'rules' => $rules, |
||
140 | 'messages' => $messages, |
||
141 | ]; |
||
142 | } |
||
143 | |||
144 | // -------------------------------------------------------------------- |
||
145 | |||
146 | /** |
||
147 | * Rules::has |
||
148 | * |
||
149 | * @param $field |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function has($field) |
||
154 | { |
||
155 | if (array_key_exists($field, $this->clauses)) { |
||
156 | return true; |
||
157 | } |
||
158 | |||
159 | return false; |
||
160 | } |
||
161 | |||
162 | // ------------------------------------------------------------------------ |
||
163 | |||
164 | /** |
||
165 | * Rules::setMessage |
||
166 | * |
||
167 | * @param string $field |
||
168 | * @param string $message |
||
169 | */ |
||
170 | public function setMessage($field, $message) |
||
171 | { |
||
172 | $this->customErrors[ $field ] = $message; |
||
173 | } |
||
174 | |||
175 | // ------------------------------------------------------------------------ |
||
176 | |||
177 | /** |
||
178 | * Rules::validate |
||
179 | * |
||
180 | * @return bool |
||
181 | * @throws \O2System\Spl\Exceptions\Logic\OutOfRangeException |
||
182 | */ |
||
183 | public function validate() |
||
184 | { |
||
185 | if (count($this->sourceVars) == 0) { |
||
186 | $this->addError(1, language()->getLine('SECURITY_RULES_E_DATA_SOURCE_EMPTY')); |
||
187 | |||
188 | return false; |
||
189 | } |
||
190 | |||
191 | foreach ($this->clauses as $fieldName => $fieldParams) { |
||
192 | |||
193 | /* Throw exception if existed rules field not yet exists in data source */ |
||
194 | if ( ! array_key_exists($fieldName, $this->sourceVars)) { |
||
195 | throw new OutOfRangeException('SECURITY_RULES_E_HEADER_OUTOFRANGEEXCEPTION', 1); |
||
196 | } |
||
197 | |||
198 | if (is_string($fieldParams[ 'rules' ])) { |
||
199 | /** |
||
200 | * Explode field rules by | as delimiter |
||
201 | */ |
||
202 | $fieldRules = explode('|', $fieldParams[ 'rules' ]); |
||
203 | |||
204 | foreach ($fieldRules as $fieldRuleMethod) { |
||
205 | /* Get parameter from given data */ |
||
206 | $fieldValue = $this->sourceVars[ $fieldName ]; |
||
207 | if ( ! is_array($fieldValue)) { |
||
208 | $fieldValue = [$fieldValue]; |
||
209 | } |
||
210 | |||
211 | if (empty($fieldValue)) { |
||
212 | array_unshift($fieldValue, null); |
||
213 | } |
||
214 | |||
215 | /* Check if rules has parameter */ |
||
216 | if (preg_match_all("/\[(.*)\]/", $fieldRuleMethod, $fieldRuleParams)) { |
||
217 | |||
218 | /* Remove [] from method */ |
||
219 | $fieldRuleMethod = preg_replace("/\[.*\]/", '', $fieldRuleMethod); |
||
220 | |||
221 | /* Explode rule parameter */ |
||
222 | $fieldRuleParams = explode(',', preg_replace("/,[ ]+/", ',', $fieldRuleParams[ 1 ][ 0 ])); |
||
223 | |||
224 | if ($fieldRuleMethod === 'match') { |
||
225 | foreach ($fieldRuleParams as $fieldRuleParamKey => $fieldRuleParamValue) { |
||
226 | if (array_key_exists($fieldRuleParamValue, $this->sourceVars)) { |
||
227 | $fieldRuleParams[ $fieldRuleParamKey ] = $this->sourceVars[ $fieldRuleParamValue ]; |
||
228 | } |
||
229 | } |
||
230 | } elseif ($fieldRuleMethod === 'listed') { |
||
231 | $fieldRuleParams = [$fieldRuleParams]; |
||
232 | } |
||
233 | |||
234 | /* Merge method's param with field rule's params */ |
||
235 | $fieldValue = array_merge($fieldValue, $fieldRuleParams); |
||
236 | } |
||
237 | |||
238 | $validationClass = new Validation; |
||
239 | $validationMethod = 'is' . studlycase($fieldRuleMethod); |
||
240 | $validationStatus = false; |
||
241 | |||
242 | /* Throw exception if method not exists in validation class */ |
||
243 | if (method_exists($validationClass, $validationMethod)) { |
||
244 | $validationStatus = call_user_func_array([&$validationClass, $validationMethod], $fieldValue); |
||
245 | } elseif (function_exists($fieldRuleMethod)) { |
||
246 | $validationStatus = call_user_func_array($fieldRuleMethod, $fieldValue); |
||
247 | } elseif (is_callable($fieldRuleMethod)) { |
||
248 | $validationStatus = call_user_func_array($fieldRuleMethod, $fieldValue); |
||
249 | } |
||
250 | |||
251 | if ($validationStatus === false) { |
||
252 | if ( ! empty($fieldParams[ 'messages' ])) { |
||
253 | $message = $fieldParams[ 'messages' ]; |
||
254 | |||
255 | /* If $rule message is array, replace $message with specified message */ |
||
256 | if (is_array($fieldParams[ 'messages' ])) { |
||
257 | if (isset($fieldParams[ 'messages' ][ $fieldRuleMethod ])) { |
||
258 | $message = $fieldParams[ 'messages' ][ $fieldRuleMethod ]; |
||
259 | } else { |
||
260 | $message = $fieldParams[ 'messages' ][ $fieldName ]; |
||
261 | } |
||
262 | } |
||
263 | } elseif (array_key_exists($fieldName, $this->customErrors)) { |
||
264 | $message = $this->customErrors[ $fieldName ]; |
||
265 | } elseif (array_key_exists($fieldRuleMethod, $this->customErrors)) { |
||
266 | $message = $this->customErrors[ $fieldRuleMethod ]; |
||
267 | } else { |
||
268 | $message = 'RULE_' . strtoupper($fieldRuleMethod); |
||
269 | } |
||
270 | |||
271 | /* Replace message placeholder, :attribute, :params */ |
||
272 | $message = str_replace(':attribute', |
||
273 | (isset($fieldParams[ 'label' ]) ? $fieldParams[ 'label' ] : $fieldName), $message); |
||
274 | if (isset($fieldRuleParams) AND ! empty($fieldRuleParams[ 0 ])) { |
||
275 | $message = str_replace(':params', implode(',', $fieldRuleParams), $message); |
||
276 | } |
||
277 | |||
278 | $this->setFieldError($fieldName, language($fieldParams[ 'label' ]), |
||
279 | language($message, [$fieldValue])); |
||
280 | } |
||
281 | |||
282 | } |
||
283 | } |
||
284 | } |
||
285 | |||
286 | return empty($this->errors) ? true : false; |
||
287 | } |
||
288 | |||
289 | // -------------------------------------------------------------------------------------- |
||
290 | |||
291 | /** |
||
292 | * Rules::setFieldError |
||
293 | * |
||
294 | * @param string $field |
||
295 | * @param string $label |
||
296 | * @param string $message |
||
297 | */ |
||
298 | protected function setFieldError($field, $label, $message) |
||
299 | { |
||
300 | $this->errors[ $field ] = [ |
||
301 | 'label' => $label, |
||
302 | 'message' => $message, |
||
303 | ]; |
||
304 | } |
||
305 | |||
306 | // -------------------------------------------------------------------------------------- |
||
307 | |||
308 | /** |
||
309 | * Rules::displayErrors |
||
310 | * |
||
311 | * @return array|string |
||
312 | */ |
||
313 | public function displayErrors() |
||
326 | } |
||
327 | } |