Total Complexity | 41 |
Total Lines | 288 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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.
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 |
||
31 | class Form { |
||
32 | |||
33 | /** |
||
34 | * Generate the form opened tag |
||
35 | * @param string $path the form action path |
||
36 | * @param array $attributes the additional form attributes |
||
37 | * @param string $method the form method like 'GET', 'POST' |
||
38 | * @return string the generated form html |
||
39 | */ |
||
40 | public static function open($path = null, array $attributes = array(), $method = 'POST') { |
||
41 | if ($path) { |
||
42 | $path = get_instance()->url->appUrl($path); |
||
43 | } |
||
44 | $method = strtoupper($method); |
||
45 | $str = null; |
||
46 | $str .= '<form action = "' . $path . '" method = "' . $method . '"'; |
||
47 | if (!isset($attributes['accept-charset'])) { |
||
48 | $attributes['accept-charset'] = get_config('charset', 'UTF-8'); |
||
49 | } |
||
50 | $str .= attributes_to_string($attributes); |
||
51 | $str .= '>'; |
||
52 | //if CSRF is enabled in the configuration |
||
53 | if (get_config('csrf_enable', false) && $method == 'POST') { |
||
54 | $csrfValue = get_instance()->security->generateCSRF(); |
||
55 | $csrfName = get_config('csrf_key', 'csrf_key'); |
||
56 | $str .= self::hidden($csrfName, $csrfValue); |
||
57 | } |
||
58 | return $str; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Generate the form opened tag for multipart like to send a file |
||
63 | * @see Form::open() for more details |
||
64 | * @return string the generated multipart form html |
||
65 | */ |
||
66 | public static function openMultipart($path = null, array $attributes = array(), $method = 'POST') { |
||
67 | $attributes['enctype'] = 'multipart/form-data'; |
||
68 | return self::open($path, $attributes, $method); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Generate the form close |
||
73 | * @return string the form close html |
||
74 | */ |
||
75 | public static function close() { |
||
76 | return '</form>'; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Generate the form fieldset & legend |
||
81 | * @param string $legend the legend tag value |
||
82 | * @param array $fieldsetAttributes the fieldset additional HTML attributes |
||
83 | * @param array $legendAttributes the legend additional HTML attributes. Is used only is $legend is not empty |
||
84 | * @return string the generated fieldset value |
||
85 | */ |
||
86 | public static function fieldset($legend = '', array $fieldsetAttributes = array(), array $legendAttributes = array()) { |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Generate the fieldset close tag |
||
96 | * @return string the generated html for fieldset close |
||
97 | */ |
||
98 | public static function fieldsetClose() { |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get the error message for the given form field name. |
||
104 | * This use the form validation information to get the error information. |
||
105 | * @param string $name the form field name |
||
106 | * @return string the error message if exists and null if not |
||
107 | */ |
||
108 | public static function error($name) { |
||
109 | $return = null; |
||
110 | $obj = & get_instance(); |
||
111 | if (isset($obj->formvalidation)) { |
||
112 | $errors = $obj->formvalidation->getErrors(); |
||
113 | if (isset($errors[$name])) { |
||
114 | $return = $errors[$name]; |
||
115 | } |
||
116 | } |
||
117 | return $return; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Get the form field value |
||
122 | * @param string $name the form field name |
||
123 | * @param mixed $default the default value if can not found the given form field name |
||
124 | * @return mixed the form field value if is set, otherwise return the default value. |
||
125 | */ |
||
126 | public static function value($name, $default = null) { |
||
127 | $value = get_instance()->request->query($name); |
||
128 | if ((is_array($value) && count($value) > 0) || strlen($value) > 0) { |
||
129 | return $value; |
||
130 | } |
||
131 | return $default; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Generate the form label html content |
||
136 | * @param string $label the title of the label |
||
137 | * @param string $for the value of the label "for" attribute |
||
138 | * @param array $attributes the additional attributes to be added |
||
139 | * @return string the generated label html content |
||
140 | */ |
||
141 | public static function label($label, $for = '', array $attributes = array()) { |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Generate the form field for input like "text", "email", "password", etc. |
||
154 | * @param string $name the form field name |
||
155 | * @param mixed $value the form field value to be set |
||
156 | * @param array $attributes the additional attributes to be added in the form input |
||
157 | * @param string $type the type of the form field (password, text, submit, button, etc.) |
||
158 | * @return string the generated form field html content for the input |
||
159 | */ |
||
160 | public static function input($name, $value = null, array $attributes = array(), $type = 'text') { |
||
161 | $str = null; |
||
162 | $str .= '<input'; |
||
163 | $attributes['type'] = $type; |
||
164 | $attributes['name'] = $name; |
||
165 | $attributes['value'] = $value; |
||
166 | $str .= attributes_to_string($attributes); |
||
167 | $str .= '/>'; |
||
168 | return $str; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Generate the form field for "text" |
||
173 | * @see Form::input() for more details |
||
174 | */ |
||
175 | public static function text($name, $value = null, array $attributes = array()) { |
||
176 | return self::input($name, $value, $attributes, 'text'); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Generate the form field for "password" |
||
181 | * @see Form::input() for more details |
||
182 | */ |
||
183 | public static function password($name, $value = null, array $attributes = array()) { |
||
184 | return self::input($name, $value, $attributes, 'password'); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Generate the form field for "radio" |
||
189 | * @see Form::input() for more details |
||
190 | */ |
||
191 | public static function radio($name, $value = null, $checked = false, array $attributes = array()) { |
||
192 | if ($checked) { |
||
193 | $attributes['checked'] = true; |
||
194 | } |
||
195 | return self::input($name, $value, $attributes, 'radio'); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Generate the form field for "checkbox" |
||
200 | * @see Form::input() for more details |
||
201 | */ |
||
202 | public static function checkbox($name, $value = null, $checked = false, array $attributes = array()) { |
||
203 | if ($checked) { |
||
204 | $attributes['checked'] = true; |
||
205 | } |
||
206 | return self::input($name, $value, $attributes, 'checkbox'); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Generate the form field for "number" |
||
211 | * @see Form::input() for more details |
||
212 | */ |
||
213 | public static function number($name, $value = null, array $attributes = array()) { |
||
214 | return self::input($name, $value, $attributes, 'number'); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Generate the form field for "phone" |
||
219 | * @see Form::input() for more details |
||
220 | */ |
||
221 | public static function phone($name, $value = null, array $attributes = array()) { |
||
222 | return self::input($name, $value, $attributes, 'phone'); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Generate the form field for "email" |
||
227 | * @see Form::input() for more details |
||
228 | */ |
||
229 | public static function email($name, $value = null, array $attributes = array()) { |
||
230 | return self::input($name, $value, $attributes, 'email'); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Generate the form field for "search" |
||
235 | * @see Form::input() for more details |
||
236 | */ |
||
237 | public static function search($name, $value = null, array $attributes = array()) { |
||
238 | return self::input($name, $value, $attributes, 'search'); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Generate the form field for "hidden" |
||
243 | * @see Form::input() for more details |
||
244 | */ |
||
245 | public static function hidden($name, $value = null, array $attributes = array()) { |
||
246 | return self::input($name, $value, $attributes, 'hidden'); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Generate the form field for "file" |
||
251 | * @see Form::input() for more details |
||
252 | */ |
||
253 | public static function file($name, array $attributes = array()) { |
||
254 | return self::input($name, null, $attributes, 'file'); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Generate the form field for "button" |
||
259 | * @see Form::input() for more details |
||
260 | */ |
||
261 | public static function button($name, $value = null, array $attributes = array()) { |
||
262 | return self::input($name, $value, $attributes, 'button'); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Generate the form field for "reset" |
||
267 | * @see Form::input() for more details |
||
268 | */ |
||
269 | public static function reset($name, $value = null, array $attributes = array()) { |
||
270 | return self::input($name, $value, $attributes, 'reset'); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Generate the form field for "submit" |
||
275 | * @see Form::input() for more details |
||
276 | */ |
||
277 | public static function submit($name, $value = null, array $attributes = array()) { |
||
278 | return self::input($name, $value, $attributes, 'submit'); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Generate the form field for textarea |
||
283 | * @param string $name the name of the textarea field |
||
284 | * @param string $value the textarea field value |
||
285 | * @param array $attributes the additional attributes to be added |
||
286 | * @return string the generated textarea form html content |
||
287 | */ |
||
288 | public static function textarea($name, $value = '', array $attributes = array()) { |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Generate the form field for select |
||
299 | * @param string $name the name of the form field |
||
300 | * @param array $values the values used to populate the "option" tags |
||
301 | * @param mixed $selected the selected value in the option list |
||
302 | * @param array $attributes the additional attribute to be added |
||
303 | * @return string the generated form field html content for select |
||
304 | */ |
||
305 | public static function select($name, array $values = array(), $selected = null, array $attributes = array()) { |
||
319 | } |
||
320 | |||
321 | } |
||
322 |