Completed
Push — develop ( b2b49f...eda620 )
by Mathieu
01:53
created

FormItem::select()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 40
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 29
nc 14
nop 5
dl 0
loc 40
ccs 0
cts 27
cp 0
crap 132
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 15
    public function __construct($itemData = [])
35
    {
36 15
        foreach ($itemData as $itemProperty => $itemValue) {
37 15
            $this->$itemProperty = $itemValue;
38
        }
39 15
    }
40
41 15
    public function __get($name)
42
    {
43 15
        if (isset($this->objectHtmlValues[$name])) {
44 15
            return $this->objectHtmlValues[$name];
45
        } else {
46 15
            return null;
47
        }
48
    }
49
50 15
    public function __set($name, $value)
51
    {
52 15
        if (in_array($name, $this->objectHtmlProperties)) {
53 15
            $this->objectHtmlValues[$name] = $value;
54
        }
55 15
    }
56
57
    public function __isset($name)
58
    {
59
        return in_array($name, $this->objectHtmlProperties);
60
    }
61
62 14
    public static function input($type, $name, $value = null, $label = null, $htmlAttributes = [])
63
    {
64 14
        $itemData           = [];
65 14
        $itemData['type']   = $type;
66 14
        $itemData['name']   = $name;
67 14
        $itemData['value']  = $value;
68 14
        $itemData['label']  = $label;
69 14
        if ($label !== null && !isset($htmlAttributes['id'])) {
70 13
            $itemData['id']     = $name;
71
        }
72 14
        $itemData = array_merge($itemData, $htmlAttributes);
73
74 14
        $item = new FormItem($itemData);
75
76 14
        $output  = $item->renderLabel();
77 14
        $output .= '<input';
78 14
        $output .= $item->renderAttributes();
79 14
        $output .= '/>';
80
81 14
        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 1
    public static function file($name, $label = null, $htmlAttributes = [])
114
    {
115 1
        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('image', $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 1
    public static function textarea($name, $value, $label = '', $htmlAttributes = [])
209
    {
210 1
        $itemData           = [];
211 1
        $itemData['name']   = $name;
212 1
        $itemData['value']  = $value;
213 1
        $itemData['label']  = $label;
214 1
        if ($label !== null && !isset($htmlAttributes['id'])) {
215 1
            $itemData['id']     = $name;
216
        }
217 1
        $itemData = array_merge($itemData, $htmlAttributes);
218
219 1
        $item = new FormItem($itemData);
220
221 1
        $output  = $item->renderLabel();
222 1
        $output .= '<textarea';
223 1
        $output .= $item->renderAttributes(true);
224 1
        $output .= '>';
225 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...
226 1
        $output .= '</textarea>';
227
228 1
        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 15
    protected function renderLabel()
267
    {
268 15
        $output = '';
269 15
        if ($this->label != '') {
270 14
            $output .= '<label';
271 14
            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 14
                $output .= ' for="' . htmlentities($this->id, ENT_COMPAT, static::$encoding) . '"';
273
            }
274 14
            $output .= '>';
275 14
            $output .= $this->label;
276 14
            $output .= '</label>';
277
        }
278
279 15
        return $output;
280
    }
281
282 15
    protected function renderAttributes($skipValue = false)
283
    {
284 15
        $output = '';
285 15
        foreach ($this->objectHtmlProperties as $currentAttribute) {
286 15
            if (!($currentAttribute == 'value' && $skipValue)) {
287 15
                if ($this->$currentAttribute !== null) {
288 15
                    $output .= ' ' . $currentAttribute . '="' . htmlentities($this->$currentAttribute, ENT_COMPAT, static::$encoding) . '"';
289
                }
290
            }
291
        }
292
293 15
        return $output;
294
    }
295
}
296