Passed
Push — develop ( c386f3...b2b49f )
by Mathieu
01:40
created

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