Completed
Branch job-vuejs (8df734)
by Adam
17:32
created

Choice::getChildrenValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Services\FormBuilder\Fields;
4
5
class Choice extends Collection
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $choices = [];
11
12
    /**
13
     * @var bool
14
     */
15
    protected $multiple = true;
16
17
    /**
18
     * @var bool
19
     */
20
    protected $expanded = true;
21
22
    /**
23
     * @return array
24
     */
25
    public function getChoices()
26
    {
27
        return $this->choices;
28
    }
29
30
    /**
31
     * @param array $choices
32
     * @return $this
33
     */
34
    public function setChoices(array $choices)
35
    {
36
        $this->choices = $choices;
37
38
        return $this;
39
    }
40
41
    /**
42
     * @return bool
43
     */
44
    public function isMultiple(): bool
45
    {
46
        return $this->multiple;
47
    }
48
49
    /**
50
     * @param bool $flag
51
     * @return $this
52
     */
53
    public function setMultiple(bool $flag)
54
    {
55
        $this->multiple = $flag;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    public function isExpanded(): bool
64
    {
65
        return $this->expanded;
66
    }
67
68
    /**
69
     * @param bool $flag
70
     * @return $this
71
     */
72
    public function setExpanded(bool $flag)
73
    {
74
        $this->expanded = $flag;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Get children values as array
81
     *
82
     * @return array
83
     */
84
    public function getChildrenValues()
85
    {
86
        $values = [];
87
88
        foreach ($this->children as $checkbox) {
89
            /** @var \Coyote\Services\FormBuilder\Fields\Checkbox $checkbox */
90
            if ($checkbox->isChecked()) {
91
                $values[] = $checkbox->getValue();
92
            }
93
        }
94
95
        return $values;
96
    }
97
98
    /** @todo ability of creating multiple select */
99
    public function createChildren()
100
    {
101
        if ($this->expanded && $this->multiple) {
102
            $this->buildCheckableChildren('checkbox');
103
        }
104
105
        if ($this->expanded && !$this->multiple) {
106
            $this->buildCheckableChildren('radio');
107
        }
108
    }
109
110
    /**
111
     * @param string $type
112
     */
113
    private function buildCheckableChildren($type)
114
    {
115
        $name = $this->name . ($this->multiple ? '[]' : '');
116
117
        $checkedValues = $this->value;
118
        if ($checkedValues instanceof \Illuminate\Support\Collection) {
119
            $checkedValues = $checkedValues->all();
120
        }
121
122
        // we must cast to array datatype because $checkedValues can be null.
123
        $checkedValues = (array) $checkedValues;
124
125
        if ($this->property) {
126
            $checkedValues = array_pluck($checkedValues, $this->property);
127
        }
128
129
        foreach ($this->choices as $key => $label) {
130
            $id = str_replace('.', '_', $this->name) . '_' . $key;
131
132
            $this->children[] = $this->makeField($name, $type, $this->parent, [
133
                'is_child' => true,
134
                'label' => $label,
135
                'checked_value' => $key,
136
                'checked' => in_array($key, $checkedValues),
137
                'attr' => [
138
                    'id' => $id
139
                ],
140
                'label_attr' => [
141
                    'for' => $id
142
                ]
143
            ])
144
            ->mergeOptions($this->childAttr);
145
        }
146
    }
147
}
148