CheckboxGroup::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\CLImate\TerminalObject\Dynamic\Checkbox;
4
5
use League\CLImate\Decorator\Parser\ParserImporter;
6
use League\CLImate\Util\OutputImporter;
7
use League\CLImate\Util\UtilImporter;
8
9
class CheckboxGroup
10
{
11
    use OutputImporter, ParserImporter, UtilImporter;
12
13
    protected $checkboxes = [];
14
15
    protected $count;
16
17 16
    public function __construct(array $options)
18
    {
19 16
        foreach ($options as $key => $option) {
20 16
            $this->checkboxes[] = new Checkbox($option, $key);
21 16
        }
22
23 16
        $this->count = count($this->checkboxes);
24
25 16
        $this->checkboxes[0]->setFirst()->setCurrent();
26 16
        $this->checkboxes[$this->count - 1]->setLast();
27 16
    }
28
29 16
    public function write()
30
    {
31 16
        array_map([$this, 'writeCheckbox'], $this->checkboxes);
32 16
    }
33
34
    /**
35
     * Retrieve the checked option values
36
     *
37
     * @return array
38
     */
39 12
    public function getCheckedValues()
40
    {
41 12
        return array_values(array_map([$this, 'getValue'], $this->getChecked()));
42
    }
43
44
    /**
45
     * Set the newly selected option based on the direction
46
     *
47
     * @param string $direction 'previous' or 'next'
48
     */
49 8
    public function setCurrent($direction)
50
    {
51 8
        list($option, $key) = $this->getCurrent();
52
53 8
        $option->setCurrent(false);
54
55 8
        $new_key = $this->getCurrentKey($direction, $option, $key);
56
57 8
        $this->checkboxes[$new_key]->setCurrent();
58 8
    }
59
60
    /**
61
     * Toggle the current option's checked status
62
     */
63 12
    public function toggleCurrent()
64
    {
65 12
        list($option) = $this->getCurrent();
66
67 12
        $option->setChecked(!$option->isChecked());
68 12
    }
69
70
    /**
71
     * Get the number of checkboxes
72
     *
73
     * @return int
74
     */
75 16
    public function count()
76
    {
77 16
        return $this->count;
78
    }
79
80
    /**
81
     * Retrieve the checked options
82
     *
83
     * @return array
84
     */
85 16
    protected function getChecked()
86
    {
87 16
        return array_filter($this->checkboxes, [$this, 'isChecked']);
88
    }
89
90
    /**
91
     * Determine whether the option is checked
92
     *
93
     * @param Checkbox $option
94
     *
95
     * @return bool
96
     */
97 16
    protected function isChecked($option)
98
    {
99 16
        return $option->isChecked();
100
    }
101
102
    /**
103
     * Retrieve the option's value
104
     *
105
     * @param Checkbox $option
106
     *
107
     * @return mixed
108
     */
109 8
    protected function getValue($option)
110
    {
111 8
        return $option->getValue();
112
    }
113
114
    /**
115
     * Get the currently selected option
116
     *
117
     * @return array
118
     */
119 16
    protected function getCurrent()
120
    {
121 16
        foreach ($this->checkboxes as $key => $option) {
122 16
            if ($option->isCurrent()) {
123 16
                return [$option, $key];
124
            }
125 8
        }
126
    }
127
128
    /**
129
     * Retrieve the correct current key
130
     *
131
     * @param string $direction 'previous' or 'next'
132
     * @param Checkbox $option
133
     * @param int $key
134
     *
135
     * @return int
136
     */
137 8
    protected function getCurrentKey($direction, $option, $key)
138
    {
139 8
        $method = 'get' . ucwords($direction). 'Key';
140
141 8
        return $this->{$method}($option, $key);
142
    }
143
144
    /**
145
     * @param Checkbox $option
146
     * @param int $key
147
     *
148
     * @return int
149
     */
150
    protected function getPreviousKey($option, $key)
151
    {
152
        if ($option->isFirst()) {
153
            return count($this->checkboxes) - 1;
154
        }
155
156
        return --$key;
157
    }
158
159
    /**
160
     * @param Checkbox $option
161
     * @param int $key
162
     *
163
     * @return int
164
     */
165 8
    protected function getNextKey($option, $key)
166
    {
167 8
        if ($option->isLast()) {
168
            return 0;
169
        }
170
171 8
        return ++$key;
172
    }
173
174
    /**
175
     * @param Checkbox $checkbox
176
     */
177 16
    protected function writeCheckbox($checkbox)
178
    {
179 16
        $checkbox->util($this->util);
180 16
        $checkbox->parser($this->parser);
181
182 16
        $parsed = $this->parser->apply((string) $checkbox);
183
184 16
        if ($checkbox->isLast()) {
185 16
            $this->output->sameLine()->write($parsed);
186 16
            return;
187
        }
188
189 16
        $this->output->write($parsed);
190 16
    }
191
}
192