Completed
Push — develop ( eda620...cca4ef )
by Mathieu
01:38
created

FormItem::radio()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 5
dl 0
loc 23
ccs 16
cts 16
cp 1
crap 3
rs 9.7666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
namespace Suricate;
3
4
/**
5
 * @SuppressWarnings("StaticAccess")
6
 */
7
class FormItem
8
{
9
    public $objectHtmlProperties = [
10
        'type',
11
        'name',
12
        'id',
13
        'class',
14
        'value',
15
        'checked',
16
        'rows',
17
        'cols',
18
        'placeholder',
19
        'tabindex',
20
        'accesskey',
21
        'disabled',
22
        'spellcheck',
23
        'events',
24
        'multiple',
25
        'autocomplete',
26
        'autofocus',
27
        'required',
28
        'pattern',
29
        'min',
30
        'step',
31
        'max',
32
        'src',
33
    ];
34
    public $label;
35
    public $objectHtmlValues            = [];
36
    public static $encoding             = 'UTF-8';
37
38 21
    public function __construct($itemData = [])
39
    {
40 21
        foreach ($itemData as $itemProperty => $itemValue) {
41 21
            $this->$itemProperty = $itemValue;
42
        }
43 21
    }
44
45 21
    public function __get($name)
46
    {
47 21
        if (isset($this->objectHtmlValues[$name])) {
48 21
            return $this->objectHtmlValues[$name];
49
        } else {
50 21
            return null;
51
        }
52
    }
53
54 21
    public function __set($name, $value)
55
    {
56 21
        if (in_array($name, $this->objectHtmlProperties)) {
57 21
            $this->objectHtmlValues[$name] = $value;
58
        }
59 21
    }
60
61
    public function __isset($name)
62
    {
63
        return in_array($name, $this->objectHtmlProperties);
64
    }
65
66 19
    public static function input($type, $name, $value = null, $label = null, $htmlAttributes = [])
67
    {
68 19
        $itemData           = [];
69 19
        $itemData['type']   = $type;
70 19
        $itemData['name']   = $name;
71 19
        $itemData['value']  = $value;
72 19
        $itemData['label']  = $label;
73 19
        if ($label !== null && !isset($htmlAttributes['id'])) {
74 15
            $itemData['id']     = $name;
75
        }
76 19
        $itemData = array_merge($itemData, $htmlAttributes);
77
78 19
        $item = new FormItem($itemData);
79
80 19
        $output  = $item->renderLabel();
81 19
        $output .= '<input';
82 19
        $output .= $item->renderAttributes();
83 19
        $output .= '/>';
84
85 19
        return $output;
86
    }
87
88 1
    public static function text($name, $value = null, $label = null, $htmlAttributes = [])
89
    {
90 1
        return static::input('text', $name, $value, $label, $htmlAttributes);
91
    }
92
93 1
    public static function password($name, $value, $label = null, $htmlAttributes = [])
94
    {
95 1
        return static::input('password', $name, $value, $label, $htmlAttributes);
96
    }
97
98 1
    public static function number($name, $value, $label = null, $htmlAttributes = [])
99
    {
100 1
        return static::input('number', $name, $value, $label, $htmlAttributes);
101
    }
102
103 1
    public static function button($name, $value, $label = null, $htmlAttributes = [])
104
    {
105 1
        return static::input('button', $name, $value, $label, $htmlAttributes);
106
    }
107
108 1
    public static function checkbox($name, $value = 1, $checked = false, $label = null, $htmlAttributes = [])
109
    {
110 1
        if (!isset($htmlAttributes['checked']) && $checked) {
111 1
            $htmlAttributes['checked'] = 'checked';
112
        }
113
        
114 1
        return static::input('checkbox', $name, $value, $label, $htmlAttributes);
115
    }
116
117 1
    public static function file($name, $label = null, $htmlAttributes = [])
118
    {
119 1
        return static::input('file', $name, null, $label, $htmlAttributes);
120
    }
121
122 1
    public static function hidden($name, $value, $htmlAttributes = [])
123
    {
124 1
        return static::input('hidden', $name, $value, null, $htmlAttributes);
125
    }
126
127 1
    public static function image($name, $url, $htmlAttributes = [])
128
    {
129 1
        $htmlAttributes['src'] = $url;
130
131 1
        return static::input('image', $name, null, null, $htmlAttributes);
132
    }
133
134 1
    public static function radio($name, $availableValues = [], $value = null, $label = null, $htmlAttributes = [])
135
    {
136 1
        $itemData           = [];
137 1
        $itemData['name']   = $name;
138 1
        $itemData['value']  = $value;
139 1
        $itemData['label']  = $label;
140 1
        $itemData = array_merge($itemData, $htmlAttributes);
141
142 1
        $item = new FormItem($itemData);
143
144 1
        $output  = $item->renderLabel();
145 1
        $output .= '<div class="radio-list">';
146 1
        foreach ($availableValues as $currentValue => $currentLabel) {
147 1
            $htmlAttributes = ['id' => $name . '-' . $currentValue];
148 1
            if ($currentValue == $value) {
149 1
                $htmlAttributes['checked'] = 'checked';
150
            }
151
152 1
            $output .= '<div class="radio-item">' . FormItem::input('radio', $name, $currentValue, $currentLabel, $htmlAttributes) . '</div>';
153
        }
154 1
        $output .= '</div>';
155
156 1
        return $output;
157
    }
158
159 1
    public static function reset($value = null, $htmlAttributes = [])
160
    {
161 1
        return static::input('reset', null, $value, null, $htmlAttributes);
162
    }
163
164 1
    public static function select($name, $availableValues = [], $value = null, $label = null, $htmlAttributes = [])
165
    {
166 1
        $itemData           = [];
167 1
        $itemData['name']   = $name;
168 1
        $itemData['value']  = $value;
169 1
        $itemData['label']  = $label;
170 1
        $itemData = array_merge($itemData, $htmlAttributes);
171
172 1
        $item = new FormItem($itemData);
173
174 1
        $output  = $item->renderLabel();
175 1
        $output .= '<select';
176 1
        $output .= $item->renderAttributes(true);
177 1
        $output .= '>';
178 1
        foreach ($availableValues as $currentKey => $currentOption) {
179 1
            if (is_array($currentOption)) {
180 1
                $output .= '<optgroup label="' . htmlentities((string) $currentKey, ENT_COMPAT, static::$encoding) . '">';
181 1
                foreach ($currentOption as $subKey => $subOption) {
182 1
                    if (is_array($value)) {
183
                        $selected = in_array($subKey, $value) ? ' selected' : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $selected is dead and can be removed.
Loading history...
184
                    } else {
185 1
                        $selected = ($subKey == $value) ? ' selected' : '';
186
                    }
187 1
                    $selected = $subKey == $value ? ' selected' : '';
188 1
                    $output .= '<option value="' . htmlentities((string) $subKey, ENT_COMPAT, static::$encoding) . '"' . $selected . '>' . htmlentities((string) $subOption, ENT_COMPAT, static::$encoding) . '</option>';
189
                }
190 1
                $output .= '</optgroup>';
191
            } else {
192 1
                if (is_array($value)) {
193
                    $selected = in_array($currentKey, $value) ? ' selected' : '';
194
                } else {
195 1
                    $selected = ($currentKey == $value) ? ' selected' : '';
196
                }
197 1
                $output .= '<option value="' . htmlentities((string) $currentKey, ENT_COMPAT, static::$encoding) . '"' . $selected . '>' . htmlentities((string) $currentOption, ENT_COMPAT, static::$encoding) . '</option>';
198
            }
199
        }
200
201 1
        $output .= '</select>';
202
203 1
        return $output;
204
    }
205
206 1
    public static function submit($name, $value, $label = '', $htmlAttributes = [])
207
    {
208 1
        return static::input('submit', $name, $value, $label, $htmlAttributes);
209
    }
210
211 1
    public static function textarea($name, $value, $label = '', $htmlAttributes = [])
212
    {
213 1
        $itemData           = [];
214 1
        $itemData['name']   = $name;
215 1
        $itemData['value']  = $value;
216 1
        $itemData['label']  = $label;
217 1
        if ($label !== null && !isset($htmlAttributes['id'])) {
218 1
            $itemData['id']     = $name;
219
        }
220 1
        $itemData = array_merge($itemData, $htmlAttributes);
221
222 1
        $item = new FormItem($itemData);
223
224 1
        $output  = $item->renderLabel();
225 1
        $output .= '<textarea';
226 1
        $output .= $item->renderAttributes(true);
227 1
        $output .= '>';
228 1
        $output .= $item->value;
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist on Suricate\FormItem. Since you implemented __get, consider adding a @property annotation.
Loading history...
229 1
        $output .= '</textarea>';
230
231 1
        return $output;
232
    }
233
234 1
    public static function tel($name, $value, $label = '', $htmlAttributes = [])
235
    {
236 1
        return static::input('tel', $name, $value, $label, $htmlAttributes);
237
    }
238
239 1
    public static function url($name, $value, $label = '', $htmlAttributes = [])
240
    {
241 1
        return static::input('url', $name, $value, $label, $htmlAttributes);
242
    }
243
244 1
    public static function email($name, $value, $label = '', $htmlAttributes = [])
245
    {
246 1
        return static::input('email', $name, $value, $label, $htmlAttributes);
247
    }
248
249 1
    public static function search($name, $value, $label = '', $htmlAttributes = [])
250
    {
251 1
        return static::input('search', $name, $value, $label, $htmlAttributes);
252
    }
253
254 1
    public static function date($name, $value, $label = '', $htmlAttributes = [])
255
    {
256 1
        return static::input('date', $name, $value, $label, $htmlAttributes);
257
    }
258
259 1
    public static function dateTime($name, $value, $label = '', $htmlAttributes = [])
260
    {
261 1
        return static::input('datetime', $name, $value, $label, $htmlAttributes);
262
    }
263
264 1
    public static function time($name, $value, $label = '', $htmlAttributes = [])
265
    {
266 1
        return static::input('time', $name, $value, $label, $htmlAttributes);
267
    }
268
269 21
    protected function renderLabel()
270
    {
271 21
        $output = '';
272 21
        if ($this->label != '') {
273 18
            $output .= '<label';
274 18
            if ($this->id != '') {
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\FormItem. Since you implemented __get, consider adding a @property annotation.
Loading history...
275 17
                $output .= ' for="' . htmlentities($this->id, ENT_COMPAT, static::$encoding) . '"';
276
            }
277 18
            $output .= '>';
278 18
            $output .= $this->label;
279 18
            $output .= '</label>';
280
        }
281
282 21
        return $output;
283
    }
284
285 21
    protected function renderAttributes($skipValue = false)
286
    {
287 21
        $output = '';
288 21
        foreach ($this->objectHtmlProperties as $currentAttribute) {
289 21
            if (!($currentAttribute == 'value' && $skipValue)) {
290 21
                if ($this->$currentAttribute !== null) {
291 21
                    $output .= ' ' . $currentAttribute . '="' . htmlentities((string) $this->$currentAttribute, ENT_COMPAT, static::$encoding) . '"';
292
                }
293
            }
294
        }
295
296 21
        return $output;
297
    }
298
}
299