Passed
Push — feature/events ( 9d660f...96fd77 )
by Mathieu
02:50
created

FormItem::file()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
/**
8
 * @property string $id Formitem id attribute
9
 * @SuppressWarnings("StaticAccess")
10
 */
11
class FormItem
12
{
13
    public $objectHtmlProperties = [
14
        'type',
15
        'name',
16
        'id',
17
        'class',
18
        'value',
19
        'checked',
20
        'rows',
21
        'cols',
22
        'placeholder',
23
        'tabindex',
24
        'accesskey',
25
        'disabled',
26
        'spellcheck',
27
        'events',
28
        'multiple',
29
        'autocomplete',
30
        'autofocus',
31
        'required',
32
        'pattern',
33
        'min',
34
        'step',
35
        'max',
36
        'src'
37
    ];
38
    public $label;
39
    public $objectHtmlValues = [];
40
    public static $encoding = 'UTF-8';
41
42 22
    public function __construct($itemData = [])
43
    {
44 22
        foreach ($itemData as $itemProperty => $itemValue) {
45 22
            $this->$itemProperty = $itemValue;
46
        }
47 22
    }
48
49 21
    public function __get($name)
50
    {
51 21
        if (isset($this->objectHtmlValues[$name])) {
52 21
            return $this->objectHtmlValues[$name];
53
        }
54
55 21
        return null;
56
    }
57
58 22
    public function __set($name, $value)
59
    {
60 22
        if (in_array($name, $this->objectHtmlProperties)) {
61 22
            $this->objectHtmlValues[$name] = $value;
62
        }
63 22
    }
64
65 1
    public function __isset($name)
66
    {
67 1
        return array_key_exists($name, $this->objectHtmlValues);
68
    }
69
70 19
    public static function input(
71
        $type,
72
        $name,
73
        $value = null,
74
        $label = null,
75
        $htmlAttributes = []
76
    ) {
77 19
        $itemData = [];
78 19
        $itemData['type'] = $type;
79 19
        $itemData['name'] = $name;
80 19
        $itemData['value'] = $value;
81 19
        $itemData['label'] = $label;
82 19
        if ($label !== null && !isset($htmlAttributes['id'])) {
83 15
            $itemData['id'] = $name;
84
        }
85 19
        $itemData = array_merge($itemData, $htmlAttributes);
86
87 19
        $item = new FormItem($itemData);
88
89 19
        $output = $item->renderLabel();
90 19
        $output .= '<input';
91 19
        $output .= $item->renderAttributes();
92 19
        $output .= '/>';
93
94 19
        return $output;
95
    }
96
97 1
    public static function text(
98
        $name,
99
        $value = null,
100
        $label = null,
101
        $htmlAttributes = []
102
    ) {
103 1
        return static::input('text', $name, $value, $label, $htmlAttributes);
104
    }
105
106 1
    public static function password(
107
        $name,
108
        $value,
109
        $label = null,
110
        $htmlAttributes = []
111
    ) {
112 1
        return static::input(
113 1
            'password',
114
            $name,
115
            $value,
116
            $label,
117
            $htmlAttributes
118
        );
119
    }
120
121 1
    public static function number(
122
        $name,
123
        $value,
124
        $label = null,
125
        $htmlAttributes = []
126
    ) {
127 1
        return static::input('number', $name, $value, $label, $htmlAttributes);
128
    }
129
130 1
    public static function button(
131
        $name,
132
        $value,
133
        $label = null,
134
        $htmlAttributes = []
135
    ) {
136 1
        return static::input('button', $name, $value, $label, $htmlAttributes);
137
    }
138
139 1
    public static function checkbox(
140
        $name,
141
        $value = 1,
142
        $checked = false,
143
        $label = null,
144
        $htmlAttributes = []
145
    ) {
146 1
        if (!isset($htmlAttributes['checked']) && $checked) {
147 1
            $htmlAttributes['checked'] = 'checked';
148
        }
149
150 1
        return static::input(
151 1
            'checkbox',
152
            $name,
153
            $value,
154
            $label,
155
            $htmlAttributes
156
        );
157
    }
158
159 1
    public static function file($name, $label = null, $htmlAttributes = [])
160
    {
161 1
        return static::input('file', $name, null, $label, $htmlAttributes);
162
    }
163
164 1
    public static function hidden($name, $value, $htmlAttributes = [])
165
    {
166 1
        return static::input('hidden', $name, $value, null, $htmlAttributes);
167
    }
168
169 1
    public static function image($name, $url, $htmlAttributes = [])
170
    {
171 1
        $htmlAttributes['src'] = $url;
172
173 1
        return static::input('image', $name, null, null, $htmlAttributes);
174
    }
175
176 1
    public static function radio(
177
        $name,
178
        $availableValues = [],
179
        $value = null,
180
        $label = null,
181
        $htmlAttributes = []
182
    ) {
183 1
        $itemData = [];
184 1
        $itemData['name'] = $name;
185 1
        $itemData['value'] = $value;
186 1
        $itemData['label'] = $label;
187 1
        $itemData = array_merge($itemData, $htmlAttributes);
188
189 1
        $item = new FormItem($itemData);
190
191 1
        $output = $item->renderLabel();
192 1
        $output .= '<div class="radio-list">';
193 1
        foreach ($availableValues as $currentValue => $currentLabel) {
194 1
            $htmlAttributes = ['id' => $name . '-' . $currentValue];
195 1
            if ($currentValue == $value) {
196 1
                $htmlAttributes['checked'] = 'checked';
197
            }
198
199
            $output .=
200
                '<div class="radio-item">' .
201 1
                FormItem::input(
202 1
                    'radio',
203 1
                    $name,
204 1
                    $currentValue,
205 1
                    $currentLabel,
206 1
                    $htmlAttributes
207
                ) .
208 1
                '</div>';
209
        }
210 1
        $output .= '</div>';
211
212 1
        return $output;
213
    }
214
215 1
    public static function reset($value = null, $htmlAttributes = [])
216
    {
217 1
        return static::input('reset', null, $value, null, $htmlAttributes);
218
    }
219
220 1
    public static function select(
221
        $name,
222
        $availableValues = [],
223
        $value = null,
224
        $label = null,
225
        $htmlAttributes = []
226
    ) {
227 1
        $itemData = [];
228 1
        $itemData['name'] = $name;
229 1
        $itemData['value'] = $value;
230 1
        $itemData['label'] = $label;
231 1
        $itemData = array_merge($itemData, $htmlAttributes);
232
233 1
        $item = new FormItem($itemData);
234
235 1
        $output = $item->renderLabel();
236 1
        $output .= '<select';
237 1
        $output .= $item->renderAttributes(true);
238 1
        $output .= '>';
239 1
        foreach ($availableValues as $currentKey => $currentOption) {
240 1
            if (is_array($currentOption)) {
241
                $output .=
242
                    '<optgroup label="' .
243 1
                    htmlentities(
244 1
                        (string) $currentKey,
245 1
                        ENT_COMPAT,
246 1
                        static::$encoding
247
                    ) .
248 1
                    '">';
249 1
                foreach ($currentOption as $subKey => $subOption) {
250 1
                    if (is_array($value)) {
251
                        $selected = in_array($subKey, $value)
252
                            ? ' selected'
253
                            : '';
254
                    } else {
255 1
                        $selected = $subKey == $value ? ' selected' : '';
256
                    }
257
                    $output .=
258
                        '<option value="' .
259 1
                        htmlentities(
260 1
                            (string) $subKey,
261 1
                            ENT_COMPAT,
262 1
                            static::$encoding
263
                        ) .
264 1
                        '"' .
265 1
                        $selected .
266 1
                        '>' .
267 1
                        htmlentities(
268 1
                            (string) $subOption,
269 1
                            ENT_COMPAT,
270 1
                            static::$encoding
271
                        ) .
272 1
                        '</option>';
273
                }
274 1
                $output .= '</optgroup>';
275
            } else {
276 1
                if (is_array($value)) {
277 1
                    $selected = in_array($currentKey, $value)
278 1
                        ? ' selected'
279 1
                        : '';
280
                } else {
281 1
                    $selected = $currentKey == $value ? ' selected' : '';
282
                }
283
                $output .=
284
                    '<option value="' .
285 1
                    htmlentities(
286 1
                        (string) $currentKey,
287 1
                        ENT_COMPAT,
288 1
                        static::$encoding
289
                    ) .
290 1
                    '"' .
291 1
                    $selected .
292 1
                    '>' .
293 1
                    htmlentities(
294 1
                        (string) $currentOption,
295 1
                        ENT_COMPAT,
296 1
                        static::$encoding
297
                    ) .
298 1
                    '</option>';
299
            }
300
        }
301
302 1
        $output .= '</select>';
303
304 1
        return $output;
305
    }
306
307 1
    public static function submit(
308
        $name,
309
        $value,
310
        $label = '',
311
        $htmlAttributes = []
312
    ) {
313 1
        return static::input('submit', $name, $value, $label, $htmlAttributes);
314
    }
315
316 1
    public static function textarea(
317
        $name,
318
        $value,
319
        $label = '',
320
        $htmlAttributes = []
321
    ) {
322 1
        $itemData = [];
323 1
        $itemData['name'] = $name;
324
325 1
        $itemData['label'] = $label;
326 1
        if ($label !== null && !isset($htmlAttributes['id'])) {
327 1
            $itemData['id'] = $name;
328
        }
329 1
        $itemData = array_merge($itemData, $htmlAttributes);
330
331 1
        $item = new FormItem($itemData);
332
333 1
        $output = $item->renderLabel();
334 1
        $output .= '<textarea';
335 1
        $output .= $item->renderAttributes(true);
336 1
        $output .= '>';
337 1
        $output .= $value;
338 1
        $output .= '</textarea>';
339
340 1
        return $output;
341
    }
342
343 1
    public static function tel($name, $value, $label = '', $htmlAttributes = [])
344
    {
345 1
        return static::input('tel', $name, $value, $label, $htmlAttributes);
346
    }
347
348 1
    public static function url($name, $value, $label = '', $htmlAttributes = [])
349
    {
350 1
        return static::input('url', $name, $value, $label, $htmlAttributes);
351
    }
352
353 1
    public static function email(
354
        $name,
355
        $value,
356
        $label = '',
357
        $htmlAttributes = []
358
    ) {
359 1
        return static::input('email', $name, $value, $label, $htmlAttributes);
360
    }
361
362 1
    public static function search(
363
        $name,
364
        $value,
365
        $label = '',
366
        $htmlAttributes = []
367
    ) {
368 1
        return static::input('search', $name, $value, $label, $htmlAttributes);
369
    }
370
371 1
    public static function date(
372
        $name,
373
        $value,
374
        $label = '',
375
        $htmlAttributes = []
376
    ) {
377 1
        return static::input('date', $name, $value, $label, $htmlAttributes);
378
    }
379
380 1
    public static function dateTime(
381
        $name,
382
        $value,
383
        $label = '',
384
        $htmlAttributes = []
385
    ) {
386 1
        return static::input(
387 1
            'datetime',
388
            $name,
389
            $value,
390
            $label,
391
            $htmlAttributes
392
        );
393
    }
394
395 1
    public static function time(
396
        $name,
397
        $value,
398
        $label = '',
399
        $htmlAttributes = []
400
    ) {
401 1
        return static::input('time', $name, $value, $label, $htmlAttributes);
402
    }
403
404 21
    protected function renderLabel()
405
    {
406 21
        $output = '';
407 21
        if ($this->label != '') {
408 18
            $output .= '<label';
409 18
            if ($this->id != '') {
410
                $output .=
411
                    ' for="' .
412 17
                    htmlentities($this->id, ENT_COMPAT, static::$encoding) .
413 17
                    '"';
414
            }
415 18
            $output .= '>';
416 18
            $output .= $this->label;
417 18
            $output .= '</label>';
418
        }
419
420 21
        return $output;
421
    }
422
423 21
    protected function renderAttributes($skipValue = false)
424
    {
425 21
        $output = '';
426 21
        foreach ($this->objectHtmlProperties as $currentAttribute) {
427 21
            if (!($currentAttribute == 'value' && $skipValue)) {
428 21
                if ($this->$currentAttribute !== null) {
429
                    $output .=
430
                        ' ' .
431 21
                        $currentAttribute .
432 21
                        '="' .
433 21
                        htmlentities(
434 21
                            (string) $this->$currentAttribute,
435 21
                            ENT_COMPAT,
436 21
                            static::$encoding
437
                        ) .
438 21
                        '"';
439
                }
440
            }
441
        }
442
443 21
        return $output;
444
    }
445
}
446