RadioCheckboxList   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 103
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerConfigKeys() 0 4 1
A setup() 0 6 1
B getValue() 0 38 6
B createCheckbox() 0 11 5
A createRadio() 0 11 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
     * {@inheritDoc}
11
     */
12 30
    protected function registerConfigKeys()
13
    {
14 30
        return [];
15
    }
16
17
    /**
18
     * {@inheritDoc}
19
     */
20 30
    public function setup()
21
    {
22 30
        parent::setup();
23
        
24 30
        $this->cssClass = str_replace('form-control', '', $this->cssClass);
25 30
    }
26
27
    /**
28
     * Generate a radio or checkbox list
29
     *
30
     * @param array $records
31
     * @return string
32
     */
33 21
    public function getValue(array $records = [])
34
    {
35
        // Check for options callable
36 21
        $record  = $this->getRecord($records);
37 21
        $options = $this->getOptions();
38 21
        $list    = '';
39
40
        // Add * to label if required
41 21
        if ($this->required) {
42 3
            $this->label .= ' *';
43 3
        }
44
45
        // add list items
46 21
        foreach ($options as $option => $label) {
47
            // Create Checkbox list or Radio Button
48 18
            if ($this->type === 'checkboxlist') {
49 9
                $this->createCheckbox($option, $record);
50 18
            } elseif ($this->type === 'radio') {
51 9
                $this->createRadio($option, $record);
52 9
            }
53
54
            // Disabled
55 18
            if ($this->disabled) {
56 6
                $this->input->disable();
57 6
            }
58
59
            // Set attributes and class
60 18
            $this->setAttributes();
61 18
            $this->input->addClass($this->cssClass);
62
63
            // Label after the radio button
64 18
            $list .= $this->labelAfterInput(' ' . $label);
65 21
        }
66
        
67
        // Label for the list
68 21
        $list = $this->builder->label($this->label)->addClass('d-block') . ' ' . $list;
69 21
        return $list;
70
    }
71
72
    /**
73
     * Create a checkbox
74
     *
75
     * @param mixed $value
76
     * @param mixed $record
77
     * @return void
78
     */
79 9
    protected function createCheckbox($value, $record)
80
    {
81 9
        $this->input = $this->builder->checkbox($this->fieldName . '[]')->value($value);
82
83
        // checked item
84 9
        if (!is_array($record) && $record == $value) {
85 3
            $this->input->check();
86 9
        } elseif (is_array($record) && in_array($value, $record)) {
87 3
            $this->input->check();
88 3
        }
89 9
    }
90
91
    /**
92
     * Create a radio button
93
     *
94
     * @param mixed $value
95
     * @param mixed $record
96
     * @return void
97
     */
98 9
    protected function createRadio($value, $record)
99
    {
100 9
        $this->input = $this->builder->radio($this->fieldName, $value);
101
102
        // checked item
103 9
        if ($record == $value) {
104 6
            $this->input->check();
105 6
        }
106
                
107 9
        $this->setRequired();
108 9
    }
109
}
110