Completed
Push — master ( ceb694...183c93 )
by Rudie
01:46
created

ChoiceType   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.03%

Importance

Changes 0
Metric Value
dl 0
loc 167
ccs 63
cts 67
cp 0.9403
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplate() 0 4 1
A determineChoiceField() 0 17 6
A getDefaults() 0 13 1
A createChildren() 0 21 5
A buildSelect() 0 9 1
B setDefaultClasses() 0 26 6
A buildCheckableChildren() 0 24 3
1
<?php
2
3
namespace  Kris\LaravelFormBuilder\Fields;
4
5
class ChoiceType extends ParentType
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $choiceType = 'select';
11
12
    /**
13
     * @inheritdoc
14
     */
15
    protected $valueProperty = 'selected';
16
17
    /**
18
     * @inheritdoc
19
     */
20 15
    protected function getTemplate()
21
    {
22 15
        return 'choice';
23
    }
24
25
    /**
26
     * Determine which choice type to use.
27
     *
28
     * @return string
29
     */
30 15
    protected function determineChoiceField()
31
    {
32 15
        $expanded = $this->options['expanded'];
33 15
        $multiple = $this->options['multiple'];
34
35 15
        if ($multiple) {
36 3
            $this->options['attr']['multiple'] = true;
37
        }
38
39 15
        if ($expanded && !$multiple) {
40 2
            return $this->choiceType = 'radio';
41
        }
42
43 13
        if ($expanded && $multiple) {
44 3
            return $this->choiceType = 'checkbox';
45
        }
46 10
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 15
    protected function getDefaults()
52
    {
53
        return [
54 15
            'choices' => null,
55
            'selected' => null,
56
            'expanded' => false,
57
            'multiple' => false,
58
            'choice_options' => [
59
                'wrapper' => false,
60
                'is_child' => true
61
            ]
62
        ];
63
    }
64
65
    /**
66
     * Create children depending on choice type.
67
     *
68
     * @return void
69
     */
70 14
    protected function createChildren()
71
    {
72 14
        if (($data_override = $this->getOption('data_override')) && $data_override instanceof \Closure) {
73 1
            $this->options['choices'] = $data_override($this->options['choices'], $this);
74
        }
75
        
76 14
        $this->children = [];
77 14
        $this->determineChoiceField();
78
79 14
        $fieldType = $this->formHelper->getFieldType($this->choiceType);
80
81 14
        switch ($this->choiceType) {
82 14
            case 'radio':
83 12
            case 'checkbox':
84 5
                $this->buildCheckableChildren($fieldType);
85 5
                break;
86
            default:
87 9
                $this->buildSelect($fieldType);
88 9
                break;
89
        }
90 14
    }
91
92
    /**
93
     * Build checkable children fields from choice type.
94
     *
95
     * @param string $fieldType
96
     *
97
     * @return void
98
     */
99 5
    protected function buildCheckableChildren($fieldType)
100
    {
101 5
        $multiple = $this->getOption('multiple') ? '[]' : '';
102
103 5
        foreach ((array)$this->options['choices'] as $key => $choice) {
104 4
            $id = str_replace('.', '_', $this->getNameKey()) . '_' . $key;
105 4
            $options = $this->formHelper->mergeOptions(
106 4
                $this->getOption('choice_options'),
107
                [
108 4
                    'attr'       => ['id' => $id],
109 4
                    'label_attr' => ['for' => $id],
110 4
                    'label'      => $choice,
111 4
                    'checked'    => in_array($key, (array)$this->options[$this->valueProperty]),
112 4
                    'value'      => $key
113
                ]
114
            );
115 4
            $this->children[] = new $fieldType(
116 4
                $this->name . $multiple,
117 4
                $this->choiceType,
118 4
                $this->parent,
119
                $options
120
            );
121
        }
122 5
    }
123
124
    /**
125
     * Build select field from choice.
126
     *
127
     * @param string $fieldType
128
     */
129 9
    protected function buildSelect($fieldType)
130
    {
131 9
        $this->children[] = new $fieldType(
132 9
            $this->name,
133 9
            $this->choiceType,
134 9
            $this->parent,
135 9
            $this->formHelper->mergeOptions($this->options, ['is_child' => true])
136
        );
137 9
    }
138
139
    /**
140
     * Creates default wrapper classes for the form element.
141
     *
142
     * @param array $options
143
     * @return array
144
     */
145 15
    protected function setDefaultClasses(array $options = [])
146
    {
147 15
        $defaults = parent::setDefaultClasses($options);
148 15
        $choice_type = $this->determineChoiceField();
149
150 15
        $wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.' . $choice_type . '_wrapper_class', '');
151 15
        if ($wrapper_class) {
152
            $defaults['wrapper']['class'] = (isset($defaults['wrapper']['class']) ? $defaults['wrapper']['class'] . ' ' : '') . $wrapper_class;
153
        }
154
155 15
        $choice_wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.wrapper_class', '');
156 15
        $choice_label_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.label_class', '');
157 15
        $choice_field_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.field_class', '');
158
159 15
        if ($choice_wrapper_class) {
160
            $defaults['choice_options']['wrapper']['class'] = $choice_wrapper_class;
161
        }
162 15
        if ($choice_label_class) {
163
            $defaults['choice_options']['label_attr']['class'] = $choice_label_class;
164
        }
165 15
        if ($choice_field_class) {
166
            $defaults['choice_options']['attr']['class'] = $choice_field_class;
167
        }
168
169 15
        return $defaults;
170
    }
171
}
172