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