Passed
Push — master ( b664da...0da004 )
by Marcio
02:58
created

Form::textBox()   B

Complexity

Conditions 9
Paths 129

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 18
rs 7.8138
cc 9
nc 129
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 .= (isset($params['id'])) ? " id='{$params['id']}'" : '';
48
        $o .= (isset($params['name'])) ? " name='{$params['name']}'" : '';
49
        $o .= (isset($params['class'])) ? " class='{$params['class']}'" : '';
50
        $o .= (isset($params['onsubmit'])) ? " onsubmit='{$params['onsubmit']}'" : '';
51
        $o .= (isset($params['method'])) ? " method='{$params['method']}'" : ' method="get"';
52
        $o .= (isset($params['action'])) ? " action='{$params['action']}'" : '';
53
        $o .= (isset($params['files'])) ? " enctype='multipart/form-data'" : '';
54
        $o .= (isset($params['style'])) ? " style='{$params['style']}'" : '';
55
        $o .= (isset($params['role'])) ? " role='{$params['role']}'" : '';
56
        $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : '';
57
        $o .= '>';
58
        return $o . "\n";
59
    }
60
61
    /**
62
     * closed the form
63
     *
64
     * @return string
65
     */
66
    public static function close()
67
    {
68
        return "</form>\n";
69
    }
70
71
    /**
72
     * textBox
73
     *
74
     * This method creates a textarea element
75
     *
76
     * @param   array(id, name, class, onclick, columns, rows, disabled, placeholder, style, value)
77
     *
78
     * @return  string
79
     */
80
    public static function textBox($params = array())
81
    {
82
83
        if (!is_array($params)) {
84
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
85
        }
86
        $o = '<textarea';
87
88
        $o .= (isset($params['cols'])) ? " cols='{$params['cols']}'" : '';
89
        $o .= (isset($params['rows'])) ? " rows='{$params['rows']}'" : '';
90
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
91
        $o .= (isset($params['maxlength'])) ? " maxlength='{$params['maxlength']}'" : '';
92
        $o .= (isset($params['style'])) ? " style='{$params['style']}'" : '';
93
        $o .= (isset($params['required'])) ? " required='required'" : '';
94
        $o .= '>';
95
        $o .= (isset($params['value'])) ? $params['value'] : '';
96
        $o .= "</textarea>\n";
97
        return $o;
98
    }
99
100
    /**
101
     * input
102
     *
103
     * This method returns a input text element.
104
     *
105
     * @param   array(id, name, class, onclick, value, length, width, disable,placeholder)
106
     *
107
     * @return  string
108
     */
109
    public static function input($params = array())
110
    {
111
112
        if (!is_array($params)) {
113
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
114
        }
115
116
        $o = '<input ';
117
        $o .= self::param_mix($params);
118
        $o .= (isset($params['onkeypress'])) ? " onkeypress='{$params['onkeypress']}'" : '';
119
        $o .= (isset($params['length'])) ? " maxlength='{$params['length']}'" : '';
120
        $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : '';
121
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
122
        $o .= (isset($params['accept'])) ? " accept='{$params['accept']}'" : '';
123
        $o .= (isset($params['maxlength'])) ? " maxlength='{$params['maxlength']}'" : '';
124
        $o .= (isset($params['minlength'])) ? " minlength='{$params['minlength']}'" : '';
125
        $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : '';
126
        $o .= (isset($params['autofocus'])) ? " autofocus" : '';
127
        $o .= " />\n";
128
        return $o;
129
    }
130
131
    /**
132
     * select
133
     *
134
     * This method returns a select html element.
135
     * It can be given a param called value which then will be preselected
136
     * data has to be array(k=>v)
137
     *
138
     * @param   array(id, name, class, onclick, disabled)
139
     *
140
     * @return  string
141
     */
142
    public static function select($params = array())
143
    {
144
145
        if (!is_array($params)) {
146
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
147
        }
148
149
        $o = "<select";
150
        $o .= self::param_mix($params);
151
        $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : '';
152
        $o .= (isset($params['required'])) ? " required='required'" : '';
153
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
154
        $o .= ">\n";
155
        $o .= "<option value=''>Select</option>\n";
156
        if (isset($params['data']) && is_array($params['data'])) {
157
            foreach ($params['data'] as $k => $v) {
158
                if (isset($params['value']) && $params['value'] == $k) {
159
                    $o .= "<option value='{$k}' selected='selected'>{$v}</option>\n";
160
                } else {
161
                    $o .= "<option value='{$k}'>{$v}</option>\n";
162
                }
163
            }
164
            throw new \InvalidArgumentException("Arguments is not valid " . print_r($params, true));
165
166
        }
167
        $o .= "</select>\n";
168
        return $o;
169
    }
170
171
    /**
172
     * checkboxMulti
173
     *
174
     * This method returns multiple checkbox elements in order given in an array
175
     * For checking of checkbox pass checked
176
     * Each checkbox should look like array(0=>array('id'=>'1', 'name'=>'cb[]', 'value'=>'x', 'label'=>'label_text' ))
177
     *
178
     * @param   array(array(id, name, value, class, checked, disabled))
179
     *
180
     * @return  string
181
     */
182
    public static function checkbox($params = array())
183
    {
184
185
        if (!is_array($params)) {
186
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
187
        }
188
        $o = '';
189
        $x = 0;
190
        foreach ($params as $k => $v) {
191
            $v['id'] = (isset($v['id'])) ? $v['id'] : "cb_id_{$x}_" . rand(1000, 9999);
192
            $o .= "<input type='checkbox'";
193
            $o .= self::param_mix($params);
194
            $o .= (isset($v['checked'])) ? " checked='checked'" : '';
195
            $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : '';
196
            $o .= " />\n";
197
            $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : '';
198
            $x++;
199
        }
200
201
        return $o;
202
    }
203
204
    /**
205
     * radioMulti
206
     *
207
     * This method returns radio elements in order given in an array
208
     * For selection pass checked
209
     * Each radio should look like array(0=>array('id'=>'1', 'name'=>'rd[]', 'value'=>'x', 'label'=>'label_text' ))
210
     *
211
     * @param   array(array(id, name, value, class, checked, disabled, label))
212
     *
213
     * @return  string
214
     */
215
    public static function radio($params = array())
216
    {
217
218
        if (!is_array($params)) {
219
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
220
        }
221
222
        $o = '';
223
        $x = 0;
224
        foreach ($params as $k => $v) {
225
            $v['id'] = (isset($v['id'])) ? $v['id'] : "rd_id_{$x}_" . rand(1000, 9999);
226
            $o .= "<input type='radio'";
227
            $o .= self::param_mix($params);
228
            $o .= (isset($v['checked'])) ? " checked='checked'" : '';
229
            $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : '';
230
            $o .= " />\n";
231
            $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : '';
232
            $x++;
233
        }
234
235
        return $o;
236
    }
237
238
    /**
239
     * This method returns a button element given the params for settings
240
     *
241
     * @param   array(id, name, class, onclick, value, disabled)
242
     *
243
     * @return  string
244
     */
245
    public static function button($params = array())
246
    {
247
248
        if (!is_array($params)) {
249
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
250
        }
251
252
        $o = "<button type='submit'";
253
        $o .= self::param_mix($params);
254
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
255
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
256
        $o .= ">";
257
        $o .= (isset($params['iclass'])) ? "<i class='fa {$params['iclass']}'></i> " : '';
258
        $o .= "</button>\n";
259
        return $o;
260
    }
261
262
    /**
263
     * This method returns a submit button element given the params for settings
264
     *
265
     * @param   array(id, name, class, onclick, value, disabled)
266
     *
267
     * @return  string
268
     */
269
    public static function submit($params = array())
270
    {
271
272
        if (!is_array($params)) {
273
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
274
        }
275
276
        $o = '<input type="submit"';
277
        $o .= self::param_mix($params);
278
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
279
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
280
        $o .= " />\n";
281
        return $o;
282
    }
283
284
    /**
285
     * This method returns a hidden input elements given its params
286
     *
287
     * @param   array(id, name, class, value)
288
     *
289
     * @return  string
290
     */
291
    public static function hidden($params = array())
292
    {
293
294
        if (!is_array($params)) {
295
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
296
        }
297
298
        $o = '<input type="hidden"';
299
        $o .= (isset($params['id'])) ? " id='{$params['id']}'" : '';
300
        $o .= (isset($params['name'])) ? " name='{$params['name']}'" : '';
301
        $o .= (isset($params['class'])) ? " class='{$params['class']}'" : '';
302
        $o .= (isset($params['value'])) ? " value='{$params['value']}'" : '';
303
        $o .= " />\n";
304
        return $o;
305
    }
306
307
    /**
308
     * This method returns a table element given the params for settings
309
     *
310
     * @param [ 'thead' => [ 'a', 'b' ], 'tbody' => [ 'ax', 'bx'], 'class' =>  'xxxxxx', ] ;
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'thead' at position 0 could not be parsed: Unknown type name ''thead'' at position 0 in 'thead'.
Loading history...
311
     *
312
     * @return  string
313
     */
314
    public static function table($params = array())
315
    {
316
317
        if (!is_array($params)) {
318
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
319
        }
320
321
        $o = "<table ";
322
        $o .= (isset($params['class'])) ? " class='{$params['class']}'" : '';
323
        $o .= ">";
324
325
        $o .= "<thead>";
326
        $o .= "<tr>";
327
        if (isset($params['thead']) && is_array($params['thead'])) {
328
            foreach ($params['thead'] as $key => $value) {
329
330
                if (isset($params['th']) && $params['th'] == $key) {
331
                    $o .= "<th>" . $value . "</th>";
332
                } else {
333
                    $o .= "<th>" . $value . "</th>";
334
                }
335
            }
336
        }
337
        $o .= " </tr>";
338
        $o .= "</thead>";
339
340
        $o .= "<tfoot>";
341
        $o .= "<tr>";
342
        if (isset($params['tfoot']) && is_array($params['tfoot'])) {
343
            foreach ($params['tfoot'] as $key => $value) {
344
                if (isset($params['td']) && $params['td'] == $key) {
345
                    $o .= "<td>" . $value . "</td>";
346
                } else {
347
                    $o .= "<td>" . $value . "</td>";
348
                }
349
            }
350
        }
351
        $o .= " </tr>";
352
        $o .= "</tfoot>";
353
354
        $o .= "<tbody>";
355
        $o .= "<tr>";
356
        if (isset($params['tbody']) && is_array($params['tbody'])) {
357
            foreach ($params['tbody'] as $key => $value) {
358
                if (isset($params['td']) && $params['td'] == $key) {
359
                    $o .= "<td>" . $value . "</td>";
360
                } else {
361
                    $o .= "<td>" . $value . "</td>";
362
                }
363
            }
364
        }
365
        $o .= " </tr>";
366
        $o .= "</tbody>";
367
        $o .= "</table>";
368
        return $o;
369
    }
370
371
    /**
372
     *
373
     * @since 1.0.6
374
     *
375
     * @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...
376
     *
377
     * @return  string
378
     */
379
380
    private static function param_mix($params = array()) {
381
382
        if (!is_array($params)) {
383
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
384
        }
385
        $o ='';
386
        $o .= (isset($params['id'])) ? " id='{$params['id']}'" : '';
387
        $o .= (isset($params['name'])) ? " name='{$params['name']}'" : '';
388
        $o .= (isset($params['class'])) ? " class='form-input textbox {$params['class']}'" : '';
389
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
390
        $o .= (isset($params['type'])) ? " type='{$params['type']}'" : 'type="text"';
391
        return $o;
392
393
    }
394
    public static function label($params = array())
395
    {
396
        $o = "<label";
397
        $o .= (isset($params['for'])) ? " for='{$params['for']}'" : '';
398
        $o .= (isset($params['class'])) ? " class='{$params['class']}'" : '';
399
        $o .= '>';
400
        $o .= (isset($params['title']) ? $params['title'] : " ");
401
        $o .= '</label>';
402
403
        return $o;
404
    }
405
406
}
407