Passed
Push — master ( a1ca05...bcb926 )
by Mathieu
15:43
created

FormItem::file()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

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