Form::close()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 MIT 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));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
41
        }
42
        $o = '<form';
43
        $o .= (isset($params['id'])) ? " id='{$params['id']}'" : '';
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['novalidate'])) ? " {$params['novalidate']}" : '';
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));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
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 .= '>';
85
        $o .= (isset($params['value'])) ? $params['value'] : '';
86
        $o .= "</textarea>\n";
87
        return $o;
88
    }
89
90
    /**
91
     * input
92
     *
93
     * This method returns a input text element.
94
     *
95
     * @param   array(id, name, class, onclick, value, length, width, disable,placeholder)
96
     *
97
     * @return  string
98
     */
99
    public static function input($params = array())
100
    {
101
102
        if (!is_array($params)) {
103
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
104
        }
105
106
        $o = '<input ';
107
        $o .= self::param_mix($params);
108
        $o .= (isset($params['onkeypress'])) ? " onkeypress='{$params['onkeypress']}'" : '';
109
        $o .= (isset($params['length'])) ? " maxlength='{$params['length']}'" : '';
110
        $o .= (isset($params['accept'])) ? " accept='{$params['accept']}'" : '';
111
        $o .= (isset($params['maxlength'])) ? " maxlength='{$params['maxlength']}'" : '';
112
        $o .= (isset($params['minlength'])) ? " minlength='{$params['minlength']}'" : '';
113
        $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : '';
114
        $o .= (isset($params['autofocus'])) ? " autofocus" : '';
115
        $o .= " />\n";
116
        return $o;
117
    }
118
119
    /**
120
     * select
121
     *
122
     * This method returns a select html element.
123
     * It can be given a param called value which then will be preselected
124
     * data has to be array(k=>v)
125
     *
126
     * @param   array(id, name, class, onclick, disabled)
127
     *
128
     * @return  string
129
     */
130
    public static function select($params = array())
131
    {
132
        if (!is_array($params)) {
133
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
134
        }
135
136
        $o = "<select";
137
        $o .= self::param_mix($params);
138
        $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : '';
139
        $o .= ">\n";
140
        $o .= "<option value=''>Select</option>\n";
141
        if (isset($params['data']) && is_array($params['data'])) {
142
            foreach ($params['data'] as $k => $v) {
143
                if (isset($params['value']) && $params['value'] == $k) {
144
                    $o .= "<option value='{$k}' selected='selected'>{$v}</option>\n";
145
                } else {
146
                    $o .= "<option value='{$k}'>{$v}</option>\n";
147
                }
148
            } 
149
        
150
            throw new \InvalidArgumentException("Arguments is not valid " . print_r($params, true));
151
152
            
153
        }
154
        $o .= "</select>\n";
155
        return $o;
156
    }
157
158
    /**
159
     * checkboxMulti
160
     *
161
     * This method returns multiple checkbox elements in order given in an array
162
     * For checking of checkbox pass checked
163
     * Each checkbox should look like array(0=>array('id'=>'1', 'name'=>'cb[]', 'value'=>'x', 'label'=>'label_text' ))
164
     *
165
     * @param   array(array(id, name, value, class, checked, disabled))
166
     *
167
     * @return  string
168
     */
169
    public static function checkbox($params = array())
170
    {
171
172
        if (!is_array($params)) {
173
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

173
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
174
        }
175
        $o = '';
176
        $x = 0;
177
        foreach ($params as $k => $v) {
178
            $v['id'] = (isset($v['id'])) ? $v['id'] : "cb_id_{$x}_" . rand(1000, 9999);
179
            $o .= "<input type='checkbox'";
180
            $o .= self::param_mix($params);
181
            $o .= (isset($v['checked'])) ? " checked='checked'" : '';
182
            $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : '';
183
            $o .= " />\n";
184
            $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : '';
185
            $x++;
186
        }
187
188
        return $o;
189
    }
190
191
    /**
192
     * radioMulti
193
     *
194
     * This method returns radio elements in order given in an array
195
     * For selection pass checked
196
     * Each radio should look like array(0=>array('id'=>'1', 'name'=>'rd[]', 'value'=>'x', 'label'=>'label_text' ))
197
     *
198
     * @param   array(array(id, name, value, class, checked, disabled, label))
199
     *
200
     * @return  string
201
     */
202
    public static function radio($params = array())
203
    {
204
205
        if (!is_array($params)) {
206
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

206
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
207
        }
208
209
        $o = '';
210
        $x = 0;
211
        foreach ($params as $k => $v) {
212
            $v['id'] = (isset($v['id'])) ? $v['id'] : "rd_id_{$x}_" . rand(1000, 9999);
213
            $o .= "<input type='radio'";
214
            $o .= self::param_mix($params);
215
            $o .= (isset($v['checked'])) ? " checked='checked'" : '';
216
            $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : '';
217
            $o .= " />\n";
218
            $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : '';
219
            $x++;
220
        }
221
222
        return $o;
223
    }
224
225
    /**
226
     * This method returns a button element given the params for settings
227
     *
228
     * @param   array(id, name, class, onclick, value, disabled)
229
     *
230
     * @return  string
231
     */
232
    public static function button($params = array())
233
    {
234
235
        if (!is_array($params)) {
236
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

236
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
237
        }
238
239
        $o = "<button type='submit'";
240
        $o .= self::param_mix($params);
241
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
242
        $o .= ">";
243
        $o .= (isset($params['class'])) ? "<i class='fa {$params['iclass']}'></i> " : '';
244
        $o .= "</button>\n";
245
        return $o;
246
    }
247
248
    /**
249
     * This method returns a submit button element given the params for settings
250
     *
251
     * @param   array(id, name, class, onclick, value, disabled)
252
     *
253
     * @return  string
254
     */
255
    public static function submit($params = array())
256
    {
257
258
        if (!is_array($params)) {
259
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

259
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
260
        }
261
262
        $o = '<input type="submit"';
263
        $o .= self::param_mix($params);
264
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
265
        $o .= " />\n";
266
        return $o;
267
    }
268
269
    /**
270
     *
271
     * @since 1.0.6
272
     *
273
     * @param array
274
     *
275
     * @return  string
276
     */
277
278
    private static function param_mix($params = array()) {
279
280
        if (!is_array($params)) {
281
            throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

281
            throw new \InvalidArgumentException("Arguments is not a Array" . /** @scrutinizer ignore-type */ print_r($params, true));
Loading history...
282
        }
283
        $o ='';
284
        $o .= (isset($params['id'])) ? " id='{$params['id']}'" : '';
285
        $o .= (isset($params['name'])) ? " name='{$params['name']}'" : '';
286
        $o .= (isset($params['value'])) ? " value='{$params['value']}'" : '';
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