Select   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 80
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A inputAttributes() 0 10 3
B renderInner() 0 45 9
A renderItem() 0 6 2
1
<?php namespace Rocket\UI\Forms\Fields;
2
3
/**
4
 * Adds a select box
5
 *
6
 * @method $this values(array $config)
7
 */
8
class Select extends Field
9
{
10
    /**
11
     * Adds attributes to the field
12
     */
13
    protected function inputAttributes()
14
    {
15
        $this->input_attributes['id'] = $this->id;
16
        $this->input_attributes['name'] = $this->name;
17
        if (array_key_exists('enabled', $this->params)
18
            && $this->params['enabled'] == false
19
        ) {
20
            $this->input_attributes['disabled'] = 'disabled';
21
        }
22
    }
23
24
    /**
25
     * Renders the box
26
     */
27
    protected function renderInner()
28
    {
29
        $options = $this->params['values'];
30
        $selected = $this->params['value'];
31
32
        unset($this->input_attributes['value']);
33
34
        if (!is_array($selected)) {
35
            $selected = [$selected];
36
        }
37
38
        // If no selected state was submitted we will attempt to set it automatically
39
        if (count($selected) === 0) {
40
            // If the form name appears in the $_POST array we have a winner!
41
            if (\Request::has($this->name)) {
42
                $selected = [\Request::get($this->name)];
43
            }
44
        }
45
46
        if (count($selected) > 1) {
47
            $this->input_attributes['multiple'] = 'multiple';
48
        }
49
50
        $form = '<select ' . $this->renderAttributes($this->input_attributes) . '>';
51
52
        foreach ($options as $key => $val) {
53
            $key = (string) $key;
54
55
            if (is_array($val) && !empty($val)) {
56
                $form .= '<optgroup label="' . $key . '">';
57
58
                foreach ($val as $optgroup_key => $optgroup_val) {
59
                    $form .= $this->renderItem($optgroup_key, $optgroup_val, in_array($optgroup_key, $selected));
60
                }
61
62
                $form .= '</optgroup>';
63
            } else {
64
                $form .= $this->renderItem($key, $val, in_array($key, $selected));
65
            }
66
        }
67
68
        $form .= '</select>';
69
70
        $this->result .= $form;
71
    }
72
73
    /**
74
     * Renders an option
75
     *
76
     * @param $key string
77
     * @param $value string
78
     * @param $selected boolean
79
     * @return string
80
     */
81
    private function renderItem($key, $value, $selected)
82
    {
83
        $value = strval($value);
84
85
        return "<option value='$key'" . ($selected ? ' selected="selected"' : '') . ">$value</option>";
86
    }
87
}
88