Total Complexity | 62 |
Total Lines | 568 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TemplateVariableInput 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 TemplateVariableInput, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TemplateVariableInput |
||
17 | { |
||
18 | /** @var string */ |
||
19 | protected $type = ''; |
||
20 | |||
21 | /** @var array */ |
||
22 | protected $input_properties = []; |
||
23 | |||
24 | /** |
||
25 | * TemplateVariableInput constructor. |
||
26 | * @param string $type ~ default options: |
||
27 | * autotag, checkbox, date, listbox, listbox-multiple, email, file, |
||
28 | * hidden, image, number, option [radio], resourcelist, richtext, tag, text, textarea, url |
||
29 | * @see https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/template-variable-input-types |
||
30 | * See manager/templates/default/element/tv/renders/input/ files for related code |
||
31 | */ |
||
32 | public function __construct(string $type) |
||
33 | { |
||
34 | $this->type = $type; |
||
35 | switch ($this->type) { |
||
36 | case 'autotag': |
||
37 | /** @var string ~ autoTag comma separated list of ints */ |
||
38 | $this->input_properties['parent_resources'] = ''; |
||
39 | break; |
||
40 | |||
41 | case 'checkbox': |
||
42 | // no break |
||
43 | case 'option':// radio |
||
44 | // checkbox, Date, listboxMulti, listboxSingle, radio |
||
45 | /** @var int */ |
||
46 | $this->input_properties['columns'] = 1; |
||
|
|||
47 | |||
48 | break; |
||
49 | |||
50 | case 'date': |
||
51 | $this->input_properties = $this->getDefaultDateProperties(); |
||
52 | break; |
||
53 | |||
54 | case 'listbox': |
||
55 | $this->input_properties = $this->getDefaultListboxProperties(); |
||
56 | break; |
||
57 | |||
58 | case 'listbox-multiple': |
||
59 | $this->input_properties = $this->getDefaultListboxProperties(); |
||
60 | /** @var bool ~ vertical or horizontal */ |
||
61 | $this->input_properties['stackItems'] = false; |
||
62 | break; |
||
63 | |||
64 | case 'number': |
||
65 | $this->input_properties = $this->getDefaultNumberProperties(); |
||
66 | break; |
||
67 | |||
68 | case 'resourcelist': |
||
69 | $this->input_properties = $this->getDefaultResourceListProperties(); |
||
70 | break; |
||
71 | |||
72 | case 'email': |
||
73 | // no break |
||
74 | case 'file': |
||
75 | // no break |
||
76 | case 'hidden': |
||
77 | // no break |
||
78 | case 'image': |
||
79 | // no break |
||
80 | case 'text': |
||
81 | // no break |
||
82 | case 'richtext': |
||
83 | // no break |
||
84 | case 'tag': |
||
85 | // no break |
||
86 | case 'textarea': |
||
87 | // no break |
||
88 | case 'url': |
||
89 | // no break |
||
90 | default: |
||
91 | $this->input_properties = $this->getDefaultTextProperties(); |
||
92 | break; |
||
93 | |||
94 | } |
||
95 | |||
96 | $this->input_properties['allowBlank'] = true; |
||
97 | |||
98 | } |
||
99 | |||
100 | public function getInputProperties() |
||
101 | { |
||
102 | return $this->input_properties; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param string $key |
||
107 | * @param mixed $value |
||
108 | * @return TemplateVariableInput |
||
109 | */ |
||
110 | public function setCustomInputProperty($key, $value): self |
||
111 | { |
||
112 | $this->input_properties[$key] = $value; |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function isAllowBlank(): bool |
||
121 | { |
||
122 | return $this->input_properties['allowBlank']; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param bool $allowBlank |
||
127 | * @return TemplateVariableInput |
||
128 | */ |
||
129 | public function setAllowBlank(bool $allowBlank): self |
||
130 | { |
||
131 | $this->input_properties['allowBlank'] = $allowBlank; |
||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param int $columns |
||
137 | * Used for type: checkbox, date, listboxMulti, listboxSingle, radio |
||
138 | * @return TemplateVariableInput |
||
139 | */ |
||
140 | public function setColumns(int $columns): self |
||
141 | { |
||
142 | $this->input_properties['columns'] = $columns; |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param string $parent_resources ~ for autotag |
||
148 | * @return TemplateVariableInput |
||
149 | */ |
||
150 | public function setParentResources(string $parent_resources): self |
||
151 | { |
||
152 | $this->input_properties['parent_resources'] = $parent_resources; |
||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param string $disabledDates |
||
158 | * @return TemplateVariableInput |
||
159 | */ |
||
160 | public function setDateDisabledDates(string $disabledDates): self |
||
161 | { |
||
162 | $this->input_properties['disabledDates'] = $disabledDates; |
||
163 | return $this; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param string $disabledDays |
||
168 | * @return TemplateVariableInput |
||
169 | */ |
||
170 | public function setDateDisabledDays(string $disabledDays): self |
||
171 | { |
||
172 | $this->input_properties['disabledDays'] = $disabledDays; |
||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $minDateValue |
||
178 | * @return TemplateVariableInput |
||
179 | */ |
||
180 | public function setDateMinDateValue(string $minDateValue): self |
||
181 | { |
||
182 | $this->input_properties['minDateValue'] = $minDateValue; |
||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param string $minTimeValue |
||
188 | * @return TemplateVariableInput |
||
189 | */ |
||
190 | public function setDateMinTimeValue(string $minTimeValue): self |
||
191 | { |
||
192 | $this->input_properties['minTimeValue'] = $minTimeValue; |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param string $maxDateValue |
||
198 | * @return TemplateVariableInput |
||
199 | */ |
||
200 | public function setDateMaxDateValue(string $maxDateValue): self |
||
201 | { |
||
202 | $this->input_properties['maxDateValue'] = $maxDateValue; |
||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param string $maxTimeValue |
||
208 | * @return TemplateVariableInput |
||
209 | */ |
||
210 | public function setDateMaxTimeValue(string $maxTimeValue): self |
||
211 | { |
||
212 | $this->input_properties['maxTimeValue'] = $maxTimeValue; |
||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param string $startDay |
||
218 | * @return TemplateVariableInput |
||
219 | */ |
||
220 | public function setDateStartDay(string $startDay): self |
||
221 | { |
||
222 | $this->input_properties['startDay'] = $startDay; |
||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param string $timeIncrement |
||
228 | * @return TemplateVariableInput |
||
229 | */ |
||
230 | public function setDateTimeIncrement(string $timeIncrement): self |
||
231 | { |
||
232 | $this->input_properties['timeIncrement'] = $timeIncrement; |
||
233 | return $this; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param bool $hideTime |
||
238 | * @return TemplateVariableInput |
||
239 | */ |
||
240 | public function setDateHideTime(bool $hideTime): self |
||
244 | } |
||
245 | |||
246 | |||
247 | /** |
||
248 | * @param string $minLength |
||
249 | * For types: email, file, hidden, image, text, richtext, tag, textarea, url |
||
250 | * @return TemplateVariableInput |
||
251 | */ |
||
252 | public function setTextMinLength(string $minLength): self |
||
253 | { |
||
254 | $this->input_properties['minLength'] = $minLength; |
||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param string $maxLength |
||
260 | * For types: email, file, hidden, image, text, richtext, tag, textarea, url |
||
261 | * @return TemplateVariableInput |
||
262 | */ |
||
263 | public function setTextMaxLength(string $maxLength): self |
||
264 | { |
||
265 | $this->input_properties['maxLength'] = $maxLength; |
||
266 | return $this; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $regex |
||
271 | * For types: email, file, hidden, image, text, richtext, tag, textarea, url |
||
272 | * @return TemplateVariableInput |
||
273 | */ |
||
274 | public function setTextRegex(string $regex): self |
||
275 | { |
||
276 | $this->input_properties['regex'] = $regex; |
||
277 | return $this; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param string $regexText |
||
282 | * For types: email, file, hidden, image, text, richtext, tag, textarea, url |
||
283 | * @return TemplateVariableInput |
||
284 | */ |
||
285 | public function setTextRegexText(string $regexText): self |
||
286 | { |
||
287 | $this->input_properties['regexText'] = $regexText; |
||
288 | return $this; |
||
289 | } |
||
290 | |||
291 | |||
292 | /** |
||
293 | * @param string $listWidth |
||
294 | * For types: listbox & listbox-multiple |
||
295 | * @return TemplateVariableInput |
||
296 | */ |
||
297 | public function setListBoxWidth(string $listWidth): self |
||
298 | { |
||
299 | $this->input_properties['listWidth'] = $listWidth; |
||
300 | return $this; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param string $title |
||
305 | * For types: listbox & listbox-multiple |
||
306 | * @return TemplateVariableInput |
||
307 | */ |
||
308 | public function setListBoxTitle(string $title): self |
||
309 | { |
||
310 | $this->input_properties['title'] = $title; |
||
311 | return $this; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param string $listEmptyText |
||
316 | * For types: listbox & listbox-multiple |
||
317 | * @return TemplateVariableInput |
||
318 | */ |
||
319 | public function setListBoxEmptyText(string $listEmptyText): self |
||
320 | { |
||
321 | $this->input_properties['listEmptyText'] = $listEmptyText; |
||
322 | return $this; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param bool $stackItems |
||
327 | * For types: listbox & listbox-multiple |
||
328 | * @return TemplateVariableInput |
||
329 | */ |
||
330 | public function setListBoxStackItems(bool $stackItems): self |
||
331 | { |
||
332 | $this->input_properties['stackItems'] = $stackItems; |
||
333 | return $this; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param bool $typeAhead |
||
338 | * For types: listbox & listbox-multiple |
||
339 | * @return TemplateVariableInput |
||
340 | */ |
||
341 | public function setListBoxTypeAhead(bool $typeAhead): self |
||
342 | { |
||
343 | $this->input_properties['typeAhead'] = $typeAhead; |
||
344 | return $this; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @param string $typeAheadDelay |
||
349 | * For types: listbox & listbox-multiple |
||
350 | * @return TemplateVariableInput |
||
351 | */ |
||
352 | public function setListBoxTypeAheadDelay(string $typeAheadDelay): self |
||
353 | { |
||
354 | $this->input_properties['typeAheadDelay'] = $typeAheadDelay; |
||
355 | return $this; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @param bool $forceSelection |
||
360 | * For types: listbox-multiple |
||
361 | * @return TemplateVariableInput |
||
362 | */ |
||
363 | public function setForceSelection(bool $forceSelection): self |
||
364 | { |
||
365 | $this->input_properties['forceSelection'] = $forceSelection; |
||
366 | return $this; |
||
367 | } |
||
368 | |||
369 | |||
370 | /** |
||
371 | * @param bool $allowDecimals |
||
372 | * For types: number |
||
373 | * @return TemplateVariableInput |
||
374 | */ |
||
375 | public function setNumberAllowDecimals(bool $allowDecimals): self |
||
376 | { |
||
377 | $this->input_properties['allowDecimals'] = $allowDecimals; |
||
378 | return $this; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @param bool $allowNegative |
||
383 | * For types: number |
||
384 | * @return TemplateVariableInput |
||
385 | */ |
||
386 | public function setNumberAllowNegative(bool $allowNegative): self |
||
387 | { |
||
388 | $this->input_properties['allowNegative'] = $allowNegative; |
||
389 | return $this; |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * @param int $decimalPrecision |
||
394 | * For types: number |
||
395 | * @return TemplateVariableInput |
||
396 | */ |
||
397 | public function setNumberDecimalPrecision(int $decimalPrecision): self |
||
398 | { |
||
399 | $this->input_properties['decimalPrecision'] = $decimalPrecision; |
||
400 | return $this; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @param string $decimalSeparator |
||
405 | * For types: number |
||
406 | * @return TemplateVariableInput |
||
407 | */ |
||
408 | public function setNumberDecimalSeparator(string $decimalSeparator): self |
||
409 | { |
||
410 | $this->input_properties['decimalSeparator'] = $decimalSeparator; |
||
411 | return $this; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @param string $maxvalue |
||
416 | * For types: number |
||
417 | * @return TemplateVariableInput |
||
418 | */ |
||
419 | public function setNumberMaxvalue(string $maxvalue): self |
||
420 | { |
||
421 | $this->input_properties['maxvalue'] = $maxvalue; |
||
422 | return $this; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @param string $minValue |
||
427 | * For types: number |
||
428 | * @return TemplateVariableInput |
||
429 | */ |
||
430 | public function setNumberMinValue(string $minValue): self |
||
431 | { |
||
432 | $this->input_properties['minValue'] = $minValue; |
||
433 | return $this; |
||
434 | } |
||
435 | |||
436 | |||
437 | /** |
||
438 | * @param bool $showNone |
||
439 | * For type: resourcelist |
||
440 | * @return TemplateVariableInput |
||
441 | */ |
||
442 | public function setResourceListShowNone(bool $showNone): self |
||
443 | { |
||
444 | $this->input_properties['showNone'] = $showNone; |
||
445 | return $this; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * @param string $parents |
||
450 | * For type: resourcelist |
||
451 | * @return TemplateVariableInput |
||
452 | */ |
||
453 | public function setResourceListParents(string $parents): self |
||
454 | { |
||
455 | $this->input_properties['parents'] = $parents; |
||
456 | return $this; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @param int $depth |
||
461 | * For type: resourcelist |
||
462 | * @return TemplateVariableInput |
||
463 | */ |
||
464 | public function setResourceListDepth(int $depth): self |
||
465 | { |
||
466 | $this->input_properties['depth'] = $depth; |
||
467 | return $this; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * @param bool $includeParent |
||
472 | * For type: resourcelist |
||
473 | * @return TemplateVariableInput |
||
474 | */ |
||
475 | public function setResourceListIncludeParent(bool $includeParent): self |
||
476 | { |
||
477 | $this->input_properties['includeParent'] = $includeParent; |
||
478 | return $this; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * @param bool $limitRelatedContext |
||
483 | * For type: resourcelist |
||
484 | * @return TemplateVariableInput |
||
485 | */ |
||
486 | public function setResourceListLimitRelatedContext(bool $limitRelatedContext): self |
||
487 | { |
||
488 | $this->input_properties['limitRelatedContext'] = $limitRelatedContext; |
||
489 | return $this; |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * @param string $where |
||
494 | * For type: resourcelist |
||
495 | * @return TemplateVariableInput |
||
496 | */ |
||
497 | public function setResourceListWhere(string $where): self |
||
498 | { |
||
499 | $this->input_properties['where'] = $where; |
||
500 | return $this; |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * @param int $limit |
||
505 | * For type: resourcelist |
||
506 | * @return TemplateVariableInput |
||
507 | */ |
||
508 | public function setResourceListLimit(int $limit): self |
||
509 | { |
||
510 | $this->input_properties['limit'] = $limit; |
||
511 | return $this; |
||
512 | } |
||
513 | |||
514 | |||
515 | protected function getDefaultDateProperties() |
||
516 | { |
||
517 | // date |
||
518 | return [ |
||
519 | 'disabledDates' => '', |
||
520 | 'disabledDays' => '', |
||
521 | // date field |
||
522 | 'minDateValue' => '', |
||
523 | // time |
||
524 | 'minTimeValue' => '', |
||
525 | // date field |
||
526 | 'maxDateValue' => '', |
||
527 | // time |
||
528 | 'maxTimeValue' => '', |
||
529 | 'startDay => ', |
||
530 | 'timeIncrement' => '', |
||
531 | 'hideTime' => false |
||
532 | ]; |
||
533 | } |
||
534 | |||
535 | protected function getDefaultListboxProperties() |
||
536 | { |
||
537 | return [ |
||
538 | // listboxMulti, listboxSingle |
||
539 | 'listWidth' => '', |
||
540 | 'title' => '', |
||
541 | 'listEmptyText' => '', |
||
542 | /** @var bool ~ require TV */ |
||
543 | 'forceSelection' => false, |
||
544 | 'typeAhead' => true, |
||
545 | 'typeAheadDelay' => '', |
||
546 | ]; |
||
547 | } |
||
548 | |||
549 | protected function getDefaultNumberProperties() |
||
550 | { |
||
551 | return [ |
||
552 | // number |
||
553 | 'allowDecimals' => true, |
||
554 | 'allowNegative' => true, |
||
555 | 'decimalPrecision' => 2, |
||
556 | 'decimalSeparator' => '.', |
||
557 | 'maxvalue' => '', |
||
558 | 'minValue' => '' |
||
559 | ]; |
||
560 | } |
||
561 | |||
562 | // email, file, hidden, image, text |
||
563 | protected function getDefaultTextProperties() |
||
570 | ]; |
||
571 | } |
||
572 | |||
573 | protected function getDefaultResourceListProperties() |
||
574 | { |
||
575 | return [ |
||
576 | // resource list |
||
577 | 'showNone' => false, |
||
578 | 'parents' => '', |
||
579 | 'depth' => 10, |
||
580 | 'includeParent' => false, |
||
581 | 'limitRelatedContext' => false, |
||
582 | 'where' => '', |
||
583 | 'limit' => 0, |
||
584 | ]; |
||
585 | } |
||
586 | |||
587 | } |