Radio::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Rocket\UI\Forms\Fields;
2
3
/**
4
 * Renders radio buttons
5
 *
6
 * @method $this checked(boolean $checked)
7
 * @method $this values(array $config)
8
 * @method $this check_value(string $value)
9
 */
10
class Radio extends Field
11
{
12
    /**
13
     * Extends the type
14
     * @param string $id
15
     * @param array $data
16
     */
17
    public function __construct($id, $data = [])
18
    {
19
        $this->type = 'radio';
20
21
        parent::__construct($id, $data);
22
    }
23
24
    /**
25
     * Adds input attributes
26
     */
27
    protected function inputAttributes()
28
    {
29
        $this->input_attributes['name'] = $this->name;
30
        $this->input_attributes['type'] = 'radio';
31
        $this->input_attributes['id'] = $this->id;
32
        $this->input_attributes['class'] = []; //removes elm
33
34
        if (array_key_exists('check_value', $this->params) && $this->params['check_value']) {
35
            $this->input_attributes['value'] = $this->params['check_value'];
36
        }
37
38
        if ($this->getValidator()->getValue($this->name) == $this->input_attributes['value']) {
39
            $this->input_attributes['checked'] = 'checked';
40
        } elseif (array_key_exists('checked', $this->params)) {
41
            if ($this->params['checked'] !== false) {
42
                $this->input_attributes['checked'] = 'checked';
43
            }
44
        }
45
46
        if (!array_key_exists('values', $this->params)) {
47
            $this->params['label_position'] = 'after';
48
        }
49
50
        if (array_key_exists('enabled', $this->params) && $this->params['enabled'] == false) {
51
            $this->input_attributes['disabled'] = 'disabled';
52
        }
53
    }
54
55
    /**
56
     * Renders the boxes
57
     */
58
    protected function renderInner()
59
    {
60
        if (array_key_exists('values', $this->params)) {
61
            unset($this->input_attributes['id']);
62
63
            $this->input_attributes['class'][] = 'form_multiple';
64
            foreach ($this->params['values'] as $key => $val) {
65
                $this->input_attributes['value'] = $key;
66
                if ($key == $this->params['value']) {
67
                    $this->input_attributes['checked'] = 'checked';
68
                } else {
69
                    unset($this->input_attributes['checked']);
70
                }
71
                $this->result .= '<label class=radio-inline>';
72
                $this->result .= '<input' . $this->renderAttributes($this->input_attributes) . ' />';
73
                $this->result .= '<span>' . $val . '</span>';
74
                $this->result .= '</label>';
75
            }
76
        } else {
77
            parent::renderInner();
78
        }
79
    }
80
81
    protected function classes()
82
    {
83
        parent::classes();
84
85
        if (!array_key_exists('values', $this->params)) {
86
            $this->label_attributes['class'][] = 'radio';
87
        }
88
    }
89
}
90