Passed
Push — master ( 23aac7...3df62e )
by Marcio
03:13
created

Form::open()   B

Complexity

Conditions 8
Paths 65

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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