Passed
Push — main ( b51461...87ea94 )
by Sammy
01:22
created

Form::labelledField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HexMakina\Marker;
6
7
/**
8
  * @method static string hidden(string $name, $value=null)
9
  * @method static string date()
10
  * @method static string time()
11
  * @method static string datetime()
12
  */
13
14
class Form
15
{
16
    public static function __callStatic(string $element_type, array $arguments): string
17
    {
18
      // arguments [name, value, [attributes]]
19
        $i = 0;
20
        $name         = $arguments[$i++] ?? null;
21
        $value        = $arguments[$i++] ?? null;
22
        $attributes   = (array)($arguments[$i++] ?? []);
23
24
        $attributes['type'] = $element_type;
25
        $attributes['name'] ??= $name;
26
        $attributes['value'] ??= $value;
27
28
        if ($attributes['type'] == 'datetime') {
29
            $attributes['type'] = 'datetime-local';
30
        } elseif ($attributes['type'] == 'password') {
31
            $attributes['value'] = '';
32
        }
33
34
        return self::input($name, $value, $attributes);
35
    }
36
37
    public static function input(string $name = null, $value = null, array $attributes = []): string
38
    {
39
        $attributes['name'] ??= $name;
40
        $attributes['value'] ??= $value;
41
42
        if (
43
              !isset($attributes['type'])
44
            || isset($attributes['disabled'])
45
            || in_array('disabled', $attributes, true)
46
        ) {
47
            // why are disabled field textual ?
48
            // radio or checkbox can be disabled too..
49
            $attributes['type'] = 'text';
50
        }
51
52
        return self::labelledField('input', null, $attributes);
53
    }
54
55
    public static function textarea(string $name, $value = null, array $attributes = []): string
56
    {
57
        $attributes['name'] ??= $name;
58
        return self::labelledField('textarea', $value, $attributes);
59
    }
60
61
    public static function select(string $name, array $option_list, $selected = null, array $attributes = []): string
62
    {
63
        $attributes['name'] ??= $name;
64
        $options = self::options($option_list, $selected);
65
        return self::labelledField('select', $options, $attributes);
66
    }
67
68
    public static function options(array $list, $selected = null): string
69
    {
70
        $options = '';
71
        foreach ($list as $value => $label) {
72
            $option_attributes = ['value' => $value];
73
            if ($selected == $value) {
74
                $option_attributes['selected'] =  'selected';
75
            }
76
77
            $options .= new Element('option', "$label", $option_attributes);
78
        }
79
80
        return $options;
81
    }
82
83
    public static function legend(string $label, array $attributes = []): string
84
    {
85
        return '' . (new Element('legend', $label, $attributes));
86
    }
87
88
    public static function label(string $for, string $label, array $attributes = []): string
89
    {
90
        $attributes['for'] = $for;
91
        unset($attributes['label']);
92
93
        return self::labelledField('label', $label, $attributes);
94
    }
95
96
    public static function submit(string $id, string $label, array $attributes = []): string
97
    {
98
        $ret = '';
99
100
        $attributes['type'] = 'submit';
101
        unset($attributes['name']);
102
103
        $attributes['id'] ??= $id;
104
        $attributes['value'] ??= $label;
105
106
        if (isset($attributes['tag']) && $attributes['tag'] === 'input') {
107
            unset($attributes['tag']);
108
            $ret .= new Element('input', '', $attributes);
109
        } else {
110
            unset($attributes['tag']);
111
            unset($attributes['value']);
112
            $ret .= new Element('button', $label, $attributes);
113
        }
114
115
        return $ret;
116
    }
117
118
    private static function labelledField(string $tag, string $content = null, array $attributes = []): string
119
    {
120
        $attributes['id'] ??= $attributes['name'] ?? '';
121
122
        $label = '';
123
        if (isset($attributes['label'])) {
124
            $label = self::label($attributes['id'], $attributes['label']);
125
            unset($attributes['label']);
126
        }
127
128
        return $label . (new Element($tag, $content, $attributes));
129
    }
130
}
131