Passed
Push — master ( 77986d...201bd6 )
by Marcio
03:08
created

Form::table()   D

Complexity

Conditions 18
Paths 17

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 55
rs 4.8666
c 0
b 0
f 0
cc 18
nc 17
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * KNUT7 K7F (https://marciozebedeu.com/)
5
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @link      https://github.com/knut7/framework/ for the canonical source repository
12
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
13
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
14
 * @author    Marcio Zebedeu - [email protected]
15
 * @version   1.0.2
16
 */
17
18
/**
19
 * Form Helper
20
 *
21
 */
22
23
namespace Ballybran\Library;
24
25
/**
26
 * Create form elements quickly.
27
 */
28
class Form implements interfaceForm
29
{
30
31
    /**
32
     * open form
33
     *
34
     * This method return the form element <form...
35
     *
36
     * @param   array(id, name, class, onsubmit, method, action, files, style)
0 ignored issues
show
Bug introduced by
The type Ballybran\Library\name was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     *
38
     * @return  string
39
     */
40
    public static function open($params = array())
41
    {
42
43
        if (!is_array($params)) {
44
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
45
        }
46
        $o = '<form';
47
        $o .= self::param_mix($params);
48
        $o .= (isset($params['onsubmit'])) ? " onsubmit='{$params['onsubmit']}'" : '';
49
        $o .= (isset($params['method'])) ? " method='{$params['method']}'" : ' method="get"';
50
        $o .= (isset($params['action'])) ? " action='{$params['action']}'" : '';
51
        $o .= (isset($params['files'])) ? " enctype='multipart/form-data'" : '';
52
        $o .= (isset($params['role'])) ? " role='{$params['role']}'" : '';
53
        $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : '';
54
        $o .= '>';
55
        return $o . "\n";
56
    }
57
58
    /**
59
     * closed the form
60
     *
61
     * @return string
62
     */
63
    public static function close()
64
    {
65
        return "</form>\n";
66
    }
67
68
    /**
69
     * textBox
70
     *
71
     * This method creates a textarea element
72
     *
73
     * @param   array(id, name, class, onclick, columns, rows, disabled, placeholder, style, value)
74
     *
75
     * @return  string
76
     */
77
    public static function textBox($params = array())
78
    {
79
80
        if (!is_array($params)) {
81
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
82
        }
83
        $o = '<textarea';
84
        $o .= self::param_mix($params);
85
        $o .= (isset($params['cols'])) ? " cols='{$params['cols']}'" : '';
86
        $o .= (isset($params['rows'])) ? " rows='{$params['rows']}'" : '';
87
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
88
        $o .= (isset($params['maxlength'])) ? " maxlength='{$params['maxlength']}'" : '';
89
        $o .= (isset($params['required'])) ? " required='required'" : '';
90
        $o .= '>';
91
        $o .= (isset($params['value'])) ? $params['value'] : '';
92
        $o .= "</textarea>\n";
93
        return $o;
94
    }
95
96
    /**
97
     * input
98
     *
99
     * This method returns a input text element.
100
     *
101
     * @param   array(id, name, class, onclick, value, length, width, disable,placeholder)
102
     *
103
     * @return  string
104
     */
105
    public static function input($params = array())
106
    {
107
108
        if (!is_array($params)) {
109
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
110
        }
111
112
        $o = '<input ';
113
        $o .= self::param_mix($params);
114
        $o .= (isset($params['onkeypress'])) ? " onkeypress='{$params['onkeypress']}'" : '';
115
        $o .= (isset($params['length'])) ? " maxlength='{$params['length']}'" : '';
116
        $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : '';
117
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
118
        $o .= (isset($params['accept'])) ? " accept='{$params['accept']}'" : '';
119
        $o .= (isset($params['maxlength'])) ? " maxlength='{$params['maxlength']}'" : '';
120
        $o .= (isset($params['minlength'])) ? " minlength='{$params['minlength']}'" : '';
121
        $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : '';
122
        $o .= (isset($params['autofocus'])) ? " autofocus" : '';
123
        $o .= " />\n";
124
        return $o;
125
    }
126
127
    /**
128
     * select
129
     *
130
     * This method returns a select html element.
131
     * It can be given a param called value which then will be preselected
132
     * data has to be array(k=>v)
133
     *
134
     * @param   array(id, name, class, onclick, disabled)
135
     *
136
     * @return  string
137
     */
138
    public static function select($params = array())
139
    {
140
141
        if (!is_array($params)) {
142
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
143
        }
144
145
        $o = "<select";
146
        $o .= self::param_mix($params);
147
        $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : '';
148
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
149
        $o .= ">\n";
150
        $o .= "<option value=''>Select</option>\n";
151
        if (isset($params['data']) && is_array($params['data'])) {
152
            foreach ($params['data'] as $k => $v) {
153
                if (isset($params['value']) && $params['value'] == $k) {
154
                    $o .= "<option value='{$k}' selected='selected'>{$v}</option>\n";
155
                } else {
156
                    $o .= "<option value='{$k}'>{$v}</option>\n";
157
                }
158
            }
159
            throw new \InvalidArgumentException("Arguments is not valid " . print_r($params, true));
160
161
        }
162
        $o .= "</select>\n";
163
        return $o;
164
    }
165
166
    /**
167
     * checkboxMulti
168
     *
169
     * This method returns multiple checkbox elements in order given in an array
170
     * For checking of checkbox pass checked
171
     * Each checkbox should look like array(0=>array('id'=>'1', 'name'=>'cb[]', 'value'=>'x', 'label'=>'label_text' ))
172
     *
173
     * @param   array(array(id, name, value, class, checked, disabled))
174
     *
175
     * @return  string
176
     */
177
    public static function checkbox($params = array())
178
    {
179
180
        if (!is_array($params)) {
181
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
182
        }
183
        $o = '';
184
        $x = 0;
185
        foreach ($params as $k => $v) {
186
            $v['id'] = (isset($v['id'])) ? $v['id'] : "cb_id_{$x}_" . rand(1000, 9999);
187
            $o .= "<input type='checkbox'";
188
            $o .= self::param_mix($params);
189
            $o .= (isset($v['checked'])) ? " checked='checked'" : '';
190
            $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : '';
191
            $o .= " />\n";
192
            $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : '';
193
            $x++;
194
        }
195
196
        return $o;
197
    }
198
199
    /**
200
     * radioMulti
201
     *
202
     * This method returns radio elements in order given in an array
203
     * For selection pass checked
204
     * Each radio should look like array(0=>array('id'=>'1', 'name'=>'rd[]', 'value'=>'x', 'label'=>'label_text' ))
205
     *
206
     * @param   array(array(id, name, value, class, checked, disabled, label))
207
     *
208
     * @return  string
209
     */
210
    public static function radio($params = array())
211
    {
212
213
        if (!is_array($params)) {
214
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
215
        }
216
217
        $o = '';
218
        $x = 0;
219
        foreach ($params as $k => $v) {
220
            $v['id'] = (isset($v['id'])) ? $v['id'] : "rd_id_{$x}_" . rand(1000, 9999);
221
            $o .= "<input type='radio'";
222
            $o .= self::param_mix($params);
223
            $o .= (isset($v['checked'])) ? " checked='checked'" : '';
224
            $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : '';
225
            $o .= " />\n";
226
            $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : '';
227
            $x++;
228
        }
229
230
        return $o;
231
    }
232
233
    /**
234
     * This method returns a button element given the params for settings
235
     *
236
     * @param   array(id, name, class, onclick, value, disabled)
237
     *
238
     * @return  string
239
     */
240
    public static function button($params = array())
241
    {
242
243
        if (!is_array($params)) {
244
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
245
        }
246
247
        $o = "<button type='submit'";
248
        $o .= self::param_mix($params);
249
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
250
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
251
        $o .= ">";
252
        $o .= (isset($params['class'])) ? "<i class='fa {$params['iclass']}'></i> " : '';
253
        $o .= "</button>\n";
254
        return $o;
255
    }
256
257
    /**
258
     * This method returns a submit button element given the params for settings
259
     *
260
     * @param   array(id, name, class, onclick, value, disabled)
261
     *
262
     * @return  string
263
     */
264
    public static function submit($params = array())
265
    {
266
267
        if (!is_array($params)) {
268
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
269
        }
270
271
        $o = '<input type="submit"';
272
        $o .= self::param_mix($params);
273
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
274
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
275
        $o .= " />\n";
276
        return $o;
277
    }
278
279
    /**
280
     *
281
     * @since 1.0.6
282
     *
283
     * @param [ 'for' => 'exemplo-title' ], ['title' => 'Example Title' ];
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'for' at position 0 could not be parsed: Unknown type name ''for'' at position 0 in 'for'.
Loading history...
284
     *
285
     * @return  string
286
     */
287
288
    private static function param_mix($params = array()) {
289
290
        if (!is_array($params)) {
291
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
292
        }
293
        $o ='';
294
        $o .= (isset($params['id'])) ? " id='{$params['id']}'" : '';
295
        $o .= (isset($params['name'])) ? " name='{$params['name']}'" : '';
296
        $o .= (isset($params['class'])) ? " class='form-input textbox {$params['class']}'" : '';
297
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
298
        $o .= (isset($params['required'])) ? " required='required'" : '';
299
        $o .= (isset($params['type'])) ? " type='{$params['type']}'" : 'type="text"';
300
        return $o;
301
302
    }
303
    public static function label($params = array())
304
    {
305
        $o = "<label";
306
        $o .= (isset($params['for'])) ? " for='{$params['for']}'" : '';
307
        $o .= (isset($params['class'])) ? " class='{$params['class']}'" : '';
308
        $o .= '>';
309
        $o .= (isset($params['title']) ? $params['title'] : " ");
310
        $o .= '</label>';
311
312
        return $o;
313
    }
314
315
}
316