Passed
Push — master ( f0fed6...922cb1 )
by Marcio
04:39 queued 01:49
created

Form::checkbox()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 20
rs 8.8333
cc 7
nc 18
nop 1
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;
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['maxlength'])) ? " maxlength='{$params['maxlength']}'" : '';
88
        $o .= (isset($params['required'])) ? " required='required'" : '';
89
        $o .= '>';
90
        $o .= (isset($params['value'])) ? $params['value'] : '';
91
        $o .= "</textarea>\n";
92
        return $o;
93
    }
94
95
    /**
96
     * input
97
     *
98
     * This method returns a input text element.
99
     *
100
     * @param   array(id, name, class, onclick, value, length, width, disable,placeholder)
101
     *
102
     * @return  string
103
     */
104
    public static function input($params = array())
105
    {
106
107
        if (!is_array($params)) {
108
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
109
        }
110
111
        $o = '<input ';
112
        $o .= self::param_mix($params);
113
        $o .= (isset($params['onkeypress'])) ? " onkeypress='{$params['onkeypress']}'" : '';
114
        $o .= (isset($params['length'])) ? " maxlength='{$params['length']}'" : '';
115
        $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : '';
116
        $o .= (isset($params['accept'])) ? " accept='{$params['accept']}'" : '';
117
        $o .= (isset($params['maxlength'])) ? " maxlength='{$params['maxlength']}'" : '';
118
        $o .= (isset($params['minlength'])) ? " minlength='{$params['minlength']}'" : '';
119
        $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : '';
120
        $o .= (isset($params['autofocus'])) ? " autofocus" : '';
121
        $o .= " />\n";
122
        return $o;
123
    }
124
125
    /**
126
     * select
127
     *
128
     * This method returns a select html element.
129
     * It can be given a param called value which then will be preselected
130
     * data has to be array(k=>v)
131
     *
132
     * @param   array(id, name, class, onclick, disabled)
133
     *
134
     * @return  string
135
     */
136
    public static function select($params = array())
137
    {
138
139
        if (!is_array($params)) {
140
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
141
        }
142
143
        $o = "<select";
144
        $o .= self::param_mix($params);
145
        $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : '';
146
        $o .= ">\n";
147
        $o .= "<option value=''>Select</option>\n";
148
        if (isset($params['data']) && is_array($params['data'])) {
149
            foreach ($params['data'] as $k => $v) {
150
                if (isset($params['value']) && $params['value'] == $k) {
151
                    $o .= "<option value='{$k}' selected='selected'>{$v}</option>\n";
152
                } else {
153
                    $o .= "<option value='{$k}'>{$v}</option>\n";
154
                }
155
            }
156
            throw new \InvalidArgumentException("Arguments is not valid " . print_r($params, true));
157
158
        }
159
        $o .= "</select>\n";
160
        return $o;
161
    }
162
163
    /**
164
     * checkboxMulti
165
     *
166
     * This method returns multiple checkbox elements in order given in an array
167
     * For checking of checkbox pass checked
168
     * Each checkbox should look like array(0=>array('id'=>'1', 'name'=>'cb[]', 'value'=>'x', 'label'=>'label_text' ))
169
     *
170
     * @param   array(array(id, name, value, class, checked, disabled))
171
     *
172
     * @return  string
173
     */
174
    public static function checkbox($params = array())
175
    {
176
177
        if (!is_array($params)) {
178
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
179
        }
180
        $o = '';
181
        $x = 0;
182
        foreach ($params as $k => $v) {
183
            $v['id'] = (isset($v['id'])) ? $v['id'] : "cb_id_{$x}_" . rand(1000, 9999);
184
            $o .= "<input type='checkbox'";
185
            $o .= self::param_mix($params);
186
            $o .= (isset($v['checked'])) ? " checked='checked'" : '';
187
            $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : '';
188
            $o .= " />\n";
189
            $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : '';
190
            $x++;
191
        }
192
193
        return $o;
194
    }
195
196
    /**
197
     * radioMulti
198
     *
199
     * This method returns radio elements in order given in an array
200
     * For selection pass checked
201
     * Each radio should look like array(0=>array('id'=>'1', 'name'=>'rd[]', 'value'=>'x', 'label'=>'label_text' ))
202
     *
203
     * @param   array(array(id, name, value, class, checked, disabled, label))
204
     *
205
     * @return  string
206
     */
207
    public static function radio($params = array())
208
    {
209
210
        if (!is_array($params)) {
211
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
212
        }
213
214
        $o = '';
215
        $x = 0;
216
        foreach ($params as $k => $v) {
217
            $v['id'] = (isset($v['id'])) ? $v['id'] : "rd_id_{$x}_" . rand(1000, 9999);
218
            $o .= "<input type='radio'";
219
            $o .= self::param_mix($params);
220
            $o .= (isset($v['checked'])) ? " checked='checked'" : '';
221
            $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : '';
222
            $o .= " />\n";
223
            $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : '';
224
            $x++;
225
        }
226
227
        return $o;
228
    }
229
230
    /**
231
     * This method returns a button element given the params for settings
232
     *
233
     * @param   array(id, name, class, onclick, value, disabled)
234
     *
235
     * @return  string
236
     */
237
    public static function button($params = array())
238
    {
239
240
        if (!is_array($params)) {
241
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
242
        }
243
244
        $o = "<button type='submit'";
245
        $o .= self::param_mix($params);
246
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
247
        $o .= ">";
248
        $o .= (isset($params['class'])) ? "<i class='fa {$params['iclass']}'></i> " : '';
249
        $o .= "</button>\n";
250
        return $o;
251
    }
252
253
    /**
254
     * This method returns a submit button element given the params for settings
255
     *
256
     * @param   array(id, name, class, onclick, value, disabled)
257
     *
258
     * @return  string
259
     */
260
    public static function submit($params = array())
261
    {
262
263
        if (!is_array($params)) {
264
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
265
        }
266
267
        $o = '<input type="submit"';
268
        $o .= self::param_mix($params);
269
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
270
        $o .= " />\n";
271
        return $o;
272
    }
273
274
    /**
275
     *
276
     * @since 1.0.6
277
     *
278
     * @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...
279
     *
280
     * @return  string
281
     */
282
283
    private static function param_mix($params = array()) {
284
285
        if (!is_array($params)) {
286
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
287
        }
288
        $o ='';
289
        $o .= (isset($params['id'])) ? " id='{$params['id']}'" : '';
290
        $o .= (isset($params['name'])) ? " name='{$params['name']}'" : '';
291
        $o .= (isset($params['class'])) ? " class='form-input textbox {$params['class']}'" : '';
292
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
293
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
294
        $o .= (isset($params['required'])) ? " required='required'" : '';
295
        $o .= (isset($params['type'])) ? " type='{$params['type']}'" : 'type="text"';
296
        return $o;
297
298
    }
299
    public static function label($params = array())
300
    {
301
        $o = "<label";
302
        $o .= (isset($params['for'])) ? " for='{$params['for']}'" : '';
303
        $o .= (isset($params['class'])) ? " class='{$params['class']}'" : '';
304
        $o .= '>';
305
        $o .= (isset($params['title']) ? $params['title'] : " ");
306
        $o .= '</label>';
307
308
        return $o;
309
    }
310
311
}
312