Checkbox   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 3
cbo 6
dl 0
loc 211
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A isCurrent() 0 4 1
A isChecked() 0 4 1
A isFirst() 0 4 1
A isLast() 0 4 1
A setCurrent() 0 6 1
A setChecked() 0 6 1
A setFirst() 0 6 1
A setLast() 0 6 1
A getValue() 0 4 1
A buildCheckboxString() 0 12 2
A getPaddingString() 0 6 1
A checkbox() 0 8 2
A pointer() 0 4 1
A __toString() 0 12 3
1
<?php
2
3
namespace League\CLImate\TerminalObject\Dynamic\Checkbox;
4
5
use League\CLImate\Decorator\Parser\ParserImporter;
6
use League\CLImate\TerminalObject\Helper\StringLength;
7
use League\CLImate\Util\UtilImporter;
8
9
class Checkbox
10
{
11
    use StringLength, ParserImporter, UtilImporter;
12
13
    /**
14
     * The value of the checkbox
15
     *
16
     * @var string|int|bool $value
17
     */
18
    protected $value;
19
20
    /**
21
     * The label for the checkbox
22
     *
23
     * @var string|int $label
24
     */
25
    protected $label;
26
27
    /**
28
     * Whether the checkbox is checked
29
     *
30
     * @var bool $checked
31
     */
32
    protected $checked = false;
33
34
    /**
35
     * Whether pointer is currently pointing at the checkbox
36
     *
37
     * @var bool $current
38
     */
39
    protected $current = false;
40
41
    /**
42
     * Whether the checkbox is the first in the group
43
     *
44
     * @var bool $first
45
     */
46
    protected $first = false;
47
48
    /**
49
     * Whether the checkbox is the last in the group
50
     *
51
     * @var bool $last
52
     */
53
    protected $last = false;
54
55 16
    public function __construct($label, $value)
56
    {
57 16
        $this->value = (!is_int($value)) ? $value : $label;
58 16
        $this->label = $label;
59 16
    }
60
61
    /**
62
     * @return bool
63
     */
64 16
    public function isCurrent()
65
    {
66 16
        return $this->current;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72 16
    public function isChecked()
73
    {
74 16
        return $this->checked;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 16
    public function isFirst()
81
    {
82 16
        return $this->first;
83
    }
84
85
    /**
86
     * @return bool
87
     */
88 16
    public function isLast()
89
    {
90 16
        return $this->last;
91
    }
92
93
    /**
94
     * Set whether the pointer is currently pointing at this checkbox
95
     *
96
     * @param bool $current
97
     *
98
     * @return Checkbox
99
     */
100 16
    public function setCurrent($current = true)
101
    {
102 16
        $this->current = $current;
103
104 16
        return $this;
105
    }
106
107
    /**
108
     * Set whether the checkbox is currently checked
109
     *
110
     * @param bool $checked
111
     *
112
     * @return Checkbox
113
     */
114 16
    public function setChecked($checked = true)
115
    {
116 16
        $this->checked = $checked;
117
118 16
        return $this;
119
    }
120
121
    /**
122
     * @return Checkbox
123
     */
124 16
    public function setFirst()
125
    {
126 16
        $this->first = true;
127
128 16
        return $this;
129
    }
130
131
    /**
132
     * @return Checkbox
133
     */
134 16
    public function setLast()
135
    {
136 16
        $this->last = true;
137
138 16
        return $this;
139
    }
140
141
    /**
142
     * @return string|int|bool
143
     */
144 12
    public function getValue()
145
    {
146 12
        return $this->value;
147
    }
148
149
    /**
150
     * Build out basic checkbox string based on current options
151
     *
152
     * @return string
153
     */
154 16
    protected function buildCheckboxString()
155
    {
156
        $parts = [
157 16
            ($this->isCurrent()) ? $this->pointer() : ' ',
158 16
            $this->checkbox($this->isChecked()),
159 16
            $this->label,
160 16
        ];
161
162 16
        $line = implode(' ', $parts);
163
164 16
        return $line . $this->getPaddingString($line);
165
    }
166
167
    /**
168
     * Get the padding string based on the length of the terminal/line
169
     *
170
     * @param string $line
171
     *
172
     * @return string
173
     */
174 16
    protected function getPaddingString($line)
175
    {
176 16
        $length = $this->util->system->width() - $this->lengthWithoutTags($line);
177
178 16
        return str_repeat(' ', $length);
179
    }
180
181
    /**
182
     * Get the checkbox symbol
183
     *
184
     * @param bool $checked
185
     *
186
     * @return string
187
     */
188 16
    protected function checkbox($checked)
189
    {
190 16
        if ($checked) {
191 16
            return html_entity_decode("&#x25CF;");
192
        }
193
194 16
        return html_entity_decode("&#x25CB;");
195
    }
196
197
    /**
198
     * Get the pointer symbol
199
     *
200
     * @return string
201
     */
202 16
    protected function pointer()
203
    {
204 16
        return html_entity_decode("&#x276F;");
205
    }
206
207 16
    public function __toString()
208
    {
209 16
        if ($this->isFirst()) {
210 16
            return $this->buildCheckboxString();
211
        }
212
213 16
        if ($this->isLast()) {
214 16
            return $this->buildCheckboxString() . $this->util->cursor->left(10) . '<hidden>';
215
        }
216
217 16
        return $this->buildCheckboxString();
218
    }
219
}
220