Completed
Push — master ( 92b672...556ced )
by Pascal
03:35
created

RadioCheckboxList::createRadio()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php namespace PascalKleindienst\FormListGenerator\Fields;
2
3
/**
4
 * Radio- and Checkbox List for Form generator
5
 * @package \PascalKleindienst\FormListGenerator\Fields
6
 */
7
class RadioCheckboxList extends AbstractField
8
{
9
    /**
10
     * @var array Key-Value Pairs for the radio button list
11
     */
12
    public $options = [];
13
14
    /**
15
     * {@inheritDoc}
16
     */
17 27
    protected function registerConfigKeys()
18
    {
19 27
        return [ 'options' ];
20
    }
21
22
    /**
23
     * {@inheritDoc}
24
     */
25 27
    public function setup()
26
    {
27 27
        parent::setup();
28
        
29 27
        $this->cssClass = str_replace('form-control', '', $this->cssClass);
30 27
    }
31
32
    /**
33
     * Generate a radio or checkbox list
34
     *
35
     * @param array $records
36
     * @return string
37
     */
38 21
    public function getValue(array $records = [])
39
    {
40
        // Check for options callable
41 21
        $record  = $this->getRecord($records);
42 21
        $options = $this->getOptions();
43 21
        $list    = '';
44
45
        // Add * to label if required
46 21
        if ($this->required) {
47 3
            $this->label .= ' *';
48 3
        }
49
50
        // add list items
51 21
        foreach ($options as $option => $label) {
52
            // Create Checkbox list or Radio Button
53 18
            if ($this->type === 'checkboxlist') {
54 9
                $this->createCheckbox($option, $record);
55 18
            } elseif ($this->type === 'radio') {
56 9
                $this->createRadio($option, $record);
57 9
            }
58
59
            // Disabled
60 18
            if ($this->disabled) {
61 6
                $this->input->disable();
62 6
            }
63
64
            // Set attributes and class
65 18
            $this->setAttributes();
66 18
            $this->input->addClass($this->cssClass);
67
68
            // Label after the radio button
69 18
            $list .= $this->labelAfterInput(' ' . $label);
70 21
        }
71
        
72
        // Label for the list
73 21
        $list = $this->builder->label($this->label)->addClass('d-block') . ' ' . $list;
74 21
        return $list;
75
    }
76
77
    /**
78
     * Create a checkbox
79
     *
80
     * @param mixed $value
81
     * @param mixed $record
82
     * @return void
83
     */
84 9
    protected function createCheckbox($value, $record)
85
    {
86 9
        $this->input = $this->builder->checkbox($this->fieldName . '[]')->value($value);
87
88
        // checked item
89 9
        if (!is_array($record) && $record == $value) {
90 3
            $this->input->check();
91 9
        } elseif (is_array($record) && in_array($value, $record)) {
92 3
            $this->input->check();
93 3
        }
94 9
    }
95
96
    /**
97
     * Create a radio button
98
     *
99
     * @param mixed $value
100
     * @param mixed $record
101
     * @return void
102
     */
103 9
    protected function createRadio($value, $record)
104
    {
105 9
        $this->input = $this->builder->radio($this->fieldName, $value);
106
107
        // checked item
108 9
        if ($record == $value) {
109 6
            $this->input->check();
110 6
        }
111
                
112 9
        $this->setRequired();
113 9
    }
114
}
115