Complex classes like Form 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Form |
||
22 | { |
||
23 | /** |
||
24 | * Utilizado para generar los id de los radio button, |
||
25 | * lleva un conteo interno. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected static $radios = array(); |
||
30 | |||
31 | /** |
||
32 | * Utilizado para avisar al programador,si usa Form::file() |
||
33 | * y no tiene el form mulipart muestra un error. |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected static $multipart = false; |
||
38 | |||
39 | /** |
||
40 | * Obtiene el valor de un componente tomado |
||
41 | * del mismo valor del nombre del campo y formulario |
||
42 | * que corresponda a un atributo del mismo nombre |
||
43 | * que sea un string, objeto o array. |
||
44 | * |
||
45 | * @param string $field |
||
46 | * @param mixed $value valor de campo |
||
47 | * @param bool $filter filtrar caracteres especiales html |
||
48 | * @param bool $check si esta marcado el checkbox |
||
49 | * @param bool $is_check |
||
50 | * |
||
51 | * @return array devuelve un array de longitud 3 con la forma array(id, name, value) |
||
52 | */ |
||
53 | public static function getField($field, $value = null, $is_check = false, $filter = true, $check = false) |
||
75 | |||
76 | /** |
||
77 | * Devuelve el valor del modelo. |
||
78 | * |
||
79 | * @param array $formField array [modelo, campo] |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | protected static function getFromModel(array $formField) |
||
93 | |||
94 | /** |
||
95 | * Devuelve el nombre y el id de un campo. |
||
96 | * |
||
97 | * @param array $field array del explode |
||
98 | * |
||
99 | * @return array array(id, name) |
||
100 | */ |
||
101 | protected static function fieldName(array $field) |
||
106 | |||
107 | /** |
||
108 | * Obtiene el valor de un componente tomado |
||
109 | * del mismo valor del nombre del campo y formulario |
||
110 | * que corresponda a un atributo del mismo nombre |
||
111 | * que sea un string, objeto o array. |
||
112 | * |
||
113 | * @param string $field |
||
114 | * @param mixed $value valor de campo |
||
115 | * @param bool $filter filtrar caracteres especiales html |
||
116 | * |
||
117 | * @return array devuelve un array de longitud 3 con la forma array(id, name, value) |
||
118 | */ |
||
119 | public static function getFieldData($field, $value = null, $filter = true) |
||
123 | |||
124 | /** |
||
125 | * Obtiene el valor de un componente check tomado |
||
126 | * del mismo valor del nombre del campo y formulario |
||
127 | * que corresponda a un atributo del mismo nombre |
||
128 | * que sea un string, objeto o array. |
||
129 | * |
||
130 | * @param string $field |
||
131 | * @param string $checkValue |
||
132 | * @param bool $checked |
||
133 | * |
||
134 | * @return array Devuelve un array de longitud 3 con la forma array(id, name, checked); |
||
135 | */ |
||
136 | public static function getFieldDataCheck($field, $checkValue, $checked = false) |
||
140 | |||
141 | /** |
||
142 | * @param string $tag |
||
143 | * @param string $field |
||
144 | * @param string $value |
||
145 | * @param string|array $attrs |
||
146 | */ |
||
147 | protected static function tag($tag, $field, $attrs = '', $value = null, $extra = '', $close = true) |
||
156 | |||
157 | /* |
||
158 | * Crea un campo input |
||
159 | * |
||
160 | * @param string|array $attrs Atributos de campo (opcional) |
||
161 | * @param string $type |
||
162 | * @param string $field |
||
163 | * @param string $value |
||
164 | * @return string |
||
165 | */ |
||
166 | public static function input($type, $field, $attrs = '', $value = null) |
||
167 | { |
||
168 | return self::tag('input', $field, $attrs, $value, "type=\"$type\" value=\"{{value}}\"", false); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Crea una etiqueta de formulario. |
||
173 | * |
||
174 | * @param string $action Acción del formulario (opcional) |
||
175 | * @param string $method Por defecto es post (opcional) |
||
176 | * @param string $attrs Atributos de etiqueta (opcional) |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public static function open($action = '', $method = 'post', $attrs = '') |
||
191 | |||
192 | /** |
||
193 | * Crea una etiqueta de formulario multipart. |
||
194 | * |
||
195 | * @param string $action Acción del formulario (opcional) |
||
196 | * @param string|array $attrs Atributos de etiqueta (opcional) |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | public static function openMultipart($action = null, $attrs = '') |
||
212 | |||
213 | /** |
||
214 | * Crea una etiqueta para cerrar un formulario. |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public static function close() |
||
224 | |||
225 | /** |
||
226 | * Crea un botón de submit para el formulario actual. |
||
227 | * |
||
228 | * @param string $text Texto del botón |
||
229 | * @param string|array $attrs Atributos de campo (opcional) |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | public static function submit($text, $attrs = '') |
||
237 | |||
238 | /** |
||
239 | * Crea un botón reset. |
||
240 | * |
||
241 | * @param string $text Texto del botón |
||
242 | * @param string|array $attrs Atributos de campo (opcional) |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public static function reset($text, $attrs = '') |
||
250 | |||
251 | /** |
||
252 | * Crea un botón. |
||
253 | * |
||
254 | * @param string $text Texto del botón |
||
255 | * @param array|string $attrs Atributos de campo (opcional) |
||
256 | * @param string $type tipo de botón |
||
257 | * @param string $value Valor para el boton |
||
258 | * |
||
259 | * @todo FALTA AGREGAR NOMBRE YA QUE SIN ESTE EL VALUE NO LLEGA AL SERVER |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public static function button($text, $attrs = '', $type = 'button', $value = null) |
||
270 | |||
271 | /** |
||
272 | * Crea un label. |
||
273 | * |
||
274 | * @param string $text Texto a mostrar |
||
275 | * @param string $field Campo al que hace referencia |
||
276 | * @param string|array $attrs Atributos de campo (opcional) |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public static function label($text, $field, $attrs = '') |
||
286 | |||
287 | /** |
||
288 | * Crea un campo text. |
||
289 | * |
||
290 | * @param string $field Nombre de campo |
||
291 | * @param string|array $attrs Atributos de campo (opcional) |
||
292 | * @param string $value (opcional) |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public static function text($field, $attrs = '', $value = null) |
||
297 | { |
||
298 | return self::input('text', $field, $attrs, $value); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Crea un campo select. |
||
303 | * |
||
304 | * @param string $field Nombre de campo |
||
305 | * @param array $data Array de valores para la lista desplegable |
||
306 | * @param string|array $attrs Atributos de campo (opcional) |
||
307 | * @param string|array $value Array para select multiple (opcional) |
||
308 | * @param string $blank agrega un item vacio si es diferente de empty |
||
309 | * @param string $itemId En caso de usar array de objeto propiedad a tomar como id |
||
310 | * @param string $show texto a mostrar, si es empty usa el to string |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | public static function select($field, $data, $attrs = '', $value = null, $blank = '', $itemId = 'id', $show = '') |
||
315 | { |
||
316 | $attrs = Tag::getAttrs($attrs); |
||
317 | // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope |
||
318 | list($id, $name, $value) = self::getFieldData($field, $value); |
||
319 | //Si se quiere agregar blank |
||
320 | $options = empty($blank) ? '' : |
||
321 | '<option value="">'.htmlspecialchars($blank, ENT_COMPAT, APP_CHARSET).'</option>'; |
||
322 | foreach ($data as $k => $v) { |
||
323 | $val = self::selectValue($v, $k, $itemId); |
||
324 | $text = self::selectShow($v, $show); |
||
325 | $selected = self::selectedValue($value, $val); |
||
326 | $options .= "<option value=\"$val\" $selected>$text</option>"; |
||
327 | } |
||
328 | |||
329 | return "<select id=\"$id\" name=\"$name\" $attrs>$options</select>"; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Retorna el value de un item de un select. |
||
334 | * |
||
335 | * @param mixed $item item de un array |
||
336 | * @param string $key valor de item dentro del select |
||
337 | * @param string $id valor posible de la propiedad del objecto para el value |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | public static function selectValue($item, $key, $id) |
||
346 | |||
347 | /** |
||
348 | * retorna el atributo para que quede seleccionado el item de un |
||
349 | * select. |
||
350 | * |
||
351 | * @param string|array $value valor(es) que deben estar seleccionados |
||
352 | * @param string $key valor del item actual |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public static function selectedValue($value, $key) |
||
361 | |||
362 | /** |
||
363 | * Retorna el valor a mostrar del item del select. |
||
364 | * |
||
365 | * @param mixed $item item del array |
||
366 | * @param string $show propiedad el objeto |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | public static function selectShow($item, $show) |
||
376 | |||
377 | /** |
||
378 | * Crea un campo checkbox. |
||
379 | * |
||
380 | * @param string $field Nombre de campo |
||
381 | * @param string $checkValue Valor en el checkbox |
||
382 | * @param string|array $attrs Atributos de campo (opcional) |
||
383 | * @param bool $checked Indica si se marca el campo (opcional) |
||
384 | * |
||
385 | * @return string |
||
386 | */ |
||
387 | public static function check($field, $checkValue, $attrs = '', $checked = false) |
||
388 | { |
||
389 | $attrs = Tag::getAttrs($attrs); |
||
390 | // Obtiene name y id para el campo y los carga en el scope |
||
391 | list($id, $name, $checked) = self::getFieldDataCheck($field, $checkValue, $checked); |
||
392 | |||
393 | if ($checked) { |
||
394 | $checked = 'checked="checked"'; |
||
395 | } |
||
396 | |||
397 | return "<input id=\"$id\" name=\"$name\" type=\"checkbox\" value=\"$checkValue\" $attrs $checked/>"; |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Crea un campo radio button. |
||
402 | * |
||
403 | * @param string $field Nombre de campo |
||
404 | * @param string $radioValue Valor en el radio |
||
405 | * @param string|array $attrs Atributos de campo (opcional) |
||
406 | * @param bool $checked Indica si se marca el campo (opcional) |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | public static function radio($field, $radioValue, $attrs = '', $checked = false) |
||
430 | |||
431 | /** |
||
432 | * Crea un botón de tipo imagen. |
||
433 | * |
||
434 | * @param string $img Nombre o ruta de la imagen |
||
435 | * @param string|array $attrs Atributos de campo (opcional) |
||
436 | * |
||
437 | * @return string |
||
438 | */ |
||
439 | public static function submitImage($img, $attrs = '') |
||
445 | |||
446 | /** |
||
447 | * Crea un campo hidden. |
||
448 | * |
||
449 | * @param string $field Nombre de campo |
||
450 | * @param string|array $attrs Atributos de campo (opcional) |
||
451 | * @param string $value |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | public static function hidden($field, $attrs = '', $value = null) |
||
456 | { |
||
457 | return self::input('hidden', $field, $attrs, $value); |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Crea un campo password. |
||
462 | * |
||
463 | * @deprecated Obsoleta desde la versión 1.0, usar password |
||
464 | * |
||
465 | * @param string $field Nombre de campo |
||
466 | * @param string|array $attrs Atributos de campo (opcional) |
||
467 | * @param string $value |
||
468 | */ |
||
469 | public static function pass($field, $attrs = '', $value = null) |
||
473 | |||
474 | /** |
||
475 | * Crea un campo passwordop. |
||
476 | * |
||
477 | * @param string $field Nombre de campo |
||
478 | * @param string|array $attrs Atributos de campo (opcional) |
||
479 | * @param string $value |
||
480 | */ |
||
481 | public static function password($field, $attrs = '', $value = null) |
||
485 | |||
486 | /** |
||
487 | * Crea un campo select que toma los valores de un array de objetos. |
||
488 | * |
||
489 | * @param string $field Nombre de campo |
||
490 | * @param string $show Campo que se mostrara (opcional) |
||
491 | * @param array $data Array('modelo','metodo','param') (opcional) |
||
492 | * @param string $blank Campo en blanco (opcional) |
||
493 | * @param string|array $attrs Atributos de campo (opcional) |
||
494 | * @param string|array $value (opcional) Array en select multiple |
||
495 | * |
||
496 | * @return string |
||
497 | */ |
||
498 | public static function dbSelect($field, $show = null, $data = null, $blank = 'Seleccione', $attrs = '', $value = null) |
||
516 | |||
517 | /** |
||
518 | * Crea un campo file. |
||
519 | * |
||
520 | * @param string $field Nombre de campo |
||
521 | * @param string|array $attrs Atributos de campo (opcional) |
||
522 | * |
||
523 | * @return string |
||
524 | */ |
||
525 | public static function file($field, $attrs = '') |
||
539 | |||
540 | /** |
||
541 | * Crea un campo textarea. |
||
542 | * |
||
543 | * @param string $field Nombre de campo |
||
544 | * @param string|array $attrs Atributos de campo (opcional) |
||
545 | * @param string $value (opcional) |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | public static function textarea($field, $attrs = '', $value = null) |
||
550 | { |
||
551 | return self::tag('textarea', $field, $attrs, $value); |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * Crea un campo fecha nativo (HTML5). |
||
556 | * |
||
557 | * @param string $field Nombre de campo |
||
558 | * @param string|array $attrs Atributos de campo (opcional) |
||
559 | * @param string $value (opcional) |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | public static function date($field, $attrs = '', $value = null) |
||
567 | |||
568 | /** |
||
569 | * Crea un campo de texo para fecha (Requiere JS ). |
||
570 | * |
||
571 | * @param string $field Nombre de campo |
||
572 | * @param string $class Clase de estilo (opcional) |
||
573 | * @param string|array $attrs Atributos de campo (opcional) |
||
574 | * @param string $value (opcional) |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | public static function datepicker($field, $class = '', $attrs = '', $value = null) |
||
582 | |||
583 | /** |
||
584 | * Crea un campo tiempo nativo (HTML5). |
||
585 | * |
||
586 | * @param string $field Nombre de campo |
||
587 | * @param string|array $attrs Atributos de campo (opcional) |
||
588 | * @param string $value (opcional) |
||
589 | * |
||
590 | * @return string |
||
591 | */ |
||
592 | public static function time($field, $attrs = '', $value = null) |
||
596 | |||
597 | /** |
||
598 | * Crea un campo fecha/tiempo nativo (HTML5). |
||
599 | * |
||
600 | * @param string $field Nombre de campo |
||
601 | * @param string|array $attrs Atributos de campo (opcional) |
||
602 | * @param string $value (opcional) |
||
603 | * |
||
604 | * @return string |
||
605 | */ |
||
606 | public static function datetime($field, $attrs = '', $value = null) |
||
610 | |||
611 | /** |
||
612 | * Crea un campo numerico nativo (HTML5). |
||
613 | * |
||
614 | * @param string $field Nombre de campo |
||
615 | * @param string|array $attrs Atributos de campo (opcional) |
||
616 | * @param string $value (opcional) |
||
617 | * |
||
618 | * @return string |
||
619 | */ |
||
620 | public static function number($field, $attrs = '', $value = null) |
||
621 | { |
||
622 | return self::input('number', $field, $attrs, $value); |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Crea un campo url nativo (HTML5). |
||
627 | * |
||
628 | * @param string $field Nombre de campo |
||
629 | * @param string|array $attrs Atributos de campo (opcional) |
||
630 | * @param string $value (opcional) |
||
631 | * |
||
632 | * @return string |
||
633 | */ |
||
634 | public static function url($field, $attrs = '', $value = null) |
||
638 | |||
639 | /** |
||
640 | * Crea un campo email nativo (HTML5). |
||
641 | * |
||
642 | * @param string $field Nombre de campo |
||
643 | * @param string|array $attrs Atributos de campo (opcional) |
||
644 | * @param string $value (opcional) |
||
645 | * |
||
646 | * @return string |
||
647 | */ |
||
648 | public static function email($field, $attrs = '', $value = null) |
||
652 | } |
||
653 |