| Total Complexity | 40 |
| Total Lines | 290 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 28 | class Form{ |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Generate the form opened tag |
||
| 32 | * @param string $path the form action path |
||
| 33 | * @param array $attributes the additional form attributes |
||
| 34 | * @param string $method the form method like 'GET', 'POST' |
||
| 35 | * @param string $enctype the form enctype like "multipart/form-data" |
||
| 36 | * @return string the generated form html |
||
| 37 | */ |
||
| 38 | public static function open($path = null, array $attributes = array(), $method = 'POST', $enctype = null){ |
||
| 39 | if($path){ |
||
| 40 | $path = Url::site_url($path); |
||
| 41 | } |
||
| 42 | $method = strtoupper($method); |
||
| 43 | $str = null; |
||
| 44 | $str .= '<form action = "'.$path.'" method = "'.$method.'"'; |
||
| 45 | if(! empty($enctype)){ |
||
| 46 | $str .= ' enctype = "'.$enctype.'" '; |
||
| 47 | } |
||
| 48 | if(! isset($attributes['accept-charset'])){ |
||
| 49 | $attributes['accept-charset'] = get_config('charset', 'utf-8'); |
||
| 50 | } |
||
| 51 | $str .= attributes_to_string($attributes); |
||
| 52 | $str .= '>'; |
||
| 53 | //if CSRF is enabled in the configuration |
||
| 54 | if(get_config('csrf_enable', false) && $method == 'POST'){ |
||
| 55 | $csrfValue = Security::generateCSRF(); |
||
| 56 | $csrfName = get_config('csrf_key', 'csrf_key'); |
||
| 57 | $str .= static::hidden($csrfName, $csrfValue); |
||
| 58 | } |
||
| 59 | return $str; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Generate the form opened tag for multipart like to send a file |
||
| 64 | * @see Form::open() for more details |
||
| 65 | * @return string the generated multipart form html |
||
| 66 | */ |
||
| 67 | public static function openMultipart($path = null, array $attributes = array(), $method = 'POST'){ |
||
| 68 | return self::open($path, $attributes, $method, 'multipart/form-data'); |
||
| 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()){ |
||
| 87 | $str = '<fieldset' . attributes_to_string($fieldsetAttributes) . '>'; |
||
| 88 | if($legend){ |
||
| 89 | $str .= '<legend' . attributes_to_string($legendAttributes) . '>'.$legend.'</legend>'; |
||
| 90 | } |
||
| 91 | return $str; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Generate the fieldset close tag |
||
| 96 | * @return string the generated html for fieldset close |
||
| 97 | */ |
||
| 98 | public static function fieldsetClose(){ |
||
| 99 | return '</fieldset>'; |
||
| 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->returnErrors(); |
||
| 113 | $error = isset($errors[$name]) ? $errors[$name] : null; |
||
| 114 | if($error){ |
||
| 115 | list($errorStart, $errorEnd) = $obj->formvalidation->getErrorDelimiter(); |
||
| 116 | $return = $errorStart . $error . $errorEnd; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | return $return; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get the form field value |
||
| 124 | * @param string $name the form field name |
||
| 125 | * @param mixed $default the default value if can not found the given form field name |
||
| 126 | * @return mixed the form field value if is set, otherwise return the default value. |
||
| 127 | */ |
||
| 128 | public static function value($name, $default = null){ |
||
| 129 | $value = get_instance()->request->query($name); |
||
| 130 | return $value ? $value : $default; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Generate the form label html content |
||
| 135 | * @param string $label the title of the label |
||
| 136 | * @param string $for the value of the label "for" attribute |
||
| 137 | * @param array $attributes the additional attributes to be added |
||
| 138 | * @return string the generated label html content |
||
| 139 | */ |
||
| 140 | public static function label($label, $for = '', array $attributes = array()){ |
||
| 141 | $str = '<label'; |
||
| 142 | if($for){ |
||
| 143 | $str .= ' for = "'.$for.'"'; |
||
| 144 | } |
||
| 145 | $str .= attributes_to_string($attributes); |
||
| 146 | $str .= '>'; |
||
| 147 | $str .= $label.'</label>'; |
||
| 148 | return $str; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Generate the form field for input like "text", "email", "password", etc. |
||
| 153 | * @param string $name the form field name |
||
| 154 | * @param mixed $value the form field value to be set |
||
| 155 | * @param array $attributes the additional attributes to be added in the form input |
||
| 156 | * @param string $type the type of the form field (password, text, submit, button, etc.) |
||
| 157 | * @return string the generated form field html content for the input |
||
| 158 | */ |
||
| 159 | public static function input($name, $value = null, array $attributes = array(), $type = 'text'){ |
||
| 160 | $str = null; |
||
| 161 | $str .= '<input name = "'.$name.'" value = "'.$value.'" type = "'.$type.'"'; |
||
| 162 | $str .= attributes_to_string($attributes); |
||
| 163 | $str .= '/>'; |
||
| 164 | return $str; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Generate the form field for "text" |
||
| 169 | * @see Form::input() for more details |
||
| 170 | */ |
||
| 171 | public static function text($name, $value = null, array $attributes = array()){ |
||
| 172 | return self::input($name, $value, $attributes, 'text'); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Generate the form field for "password" |
||
| 177 | * @see Form::input() for more details |
||
| 178 | */ |
||
| 179 | public static function password($name, $value = null, array $attributes = array()){ |
||
| 180 | return self::input($name, $value, $attributes, 'password'); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Generate the form field for "radio" |
||
| 185 | * @see Form::input() for more details |
||
| 186 | */ |
||
| 187 | public static function radio($name, $value = null, $checked = false, array $attributes = array()){ |
||
| 188 | if($checked){ |
||
| 189 | $attributes['checked'] = true; |
||
| 190 | } |
||
| 191 | return self::input($name, $value, $attributes, 'radio'); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Generate the form field for "checkbox" |
||
| 196 | * @see Form::input() for more details |
||
| 197 | */ |
||
| 198 | public static function checkbox($name, $value = null, $checked = false, array $attributes = array()){ |
||
| 199 | if($checked){ |
||
| 200 | $attributes['checked'] = true; |
||
| 201 | } |
||
| 202 | return self::input($name, $value, $attributes, 'checkbox'); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Generate the form field for "number" |
||
| 207 | * @see Form::input() for more details |
||
| 208 | */ |
||
| 209 | public static function number($name, $value = null, array $attributes = array()){ |
||
| 210 | return self::input($name, $value, $attributes, 'number'); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Generate the form field for "phone" |
||
| 215 | * @see Form::input() for more details |
||
| 216 | */ |
||
| 217 | public static function phone($name, $value = null, array $attributes = array()){ |
||
| 218 | return self::input($name, $value, $attributes, 'phone'); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Generate the form field for "email" |
||
| 223 | * @see Form::input() for more details |
||
| 224 | */ |
||
| 225 | public static function email($name, $value = null, array $attributes = array()){ |
||
| 226 | return self::input($name, $value, $attributes, 'email'); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Generate the form field for "search" |
||
| 231 | * @see Form::input() for more details |
||
| 232 | */ |
||
| 233 | public static function search($name, $value = null, array $attributes = array()){ |
||
| 234 | return self::input($name, $value, $attributes, 'search'); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Generate the form field for "hidden" |
||
| 239 | * @see Form::input() for more details |
||
| 240 | */ |
||
| 241 | public static function hidden($name, $value = null, array $attributes = array()){ |
||
| 242 | return self::input($name, $value, $attributes, 'hidden'); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Generate the form field for "file" |
||
| 247 | * @see Form::input() for more details |
||
| 248 | */ |
||
| 249 | public static function file($name, array $attributes = array()){ |
||
| 250 | return self::input($name, null, $attributes, 'file'); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Generate the form field for "button" |
||
| 255 | * @see Form::input() for more details |
||
| 256 | */ |
||
| 257 | public static function button($name, $value = null, array $attributes = array()){ |
||
| 258 | return self::input($name, $value, $attributes, 'button'); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Generate the form field for "reset" |
||
| 263 | * @see Form::input() for more details |
||
| 264 | */ |
||
| 265 | public static function reset($name, $value = null, array $attributes = array()){ |
||
| 266 | return self::input($name, $value, $attributes, 'reset'); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Generate the form field for "submit" |
||
| 271 | * @see Form::input() for more details |
||
| 272 | */ |
||
| 273 | public static function submit($name, $value = null, array $attributes = array()){ |
||
| 274 | return self::input($name, $value, $attributes, 'submit'); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Generate the form field for textarea |
||
| 279 | * @param string $name the name of the textarea field |
||
| 280 | * @param string $value the textarea field value |
||
| 281 | * @param array $attributes the additional attributes to be added |
||
| 282 | * @return string the generated textarea form html content |
||
| 283 | */ |
||
| 284 | public static function textarea($name, $value = '', array $attributes = array()){ |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Generate the form field for select |
||
| 295 | * @param string $name the name of the form field |
||
| 296 | * @param mixed|array $values the values used to populate the "option" tags |
||
| 297 | * @param mixed $selected the selected value in the option list |
||
| 298 | * @param array $attributes the additional attribute to be added |
||
| 299 | * @return string the generated form field html content for select |
||
| 300 | */ |
||
| 301 | public static function select($name, $values = null, $selected = null, array $attributes = array()){ |
||
| 318 | } |
||
| 319 | |||
| 320 | } |
||
| 321 |