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->width() - $this->lengthWithoutTags($line); |
177
|
|
|
if ($length < 1) { |
178
|
16 |
|
return ''; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return str_repeat(' ', $length); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the checkbox symbol |
186
|
|
|
* |
187
|
|
|
* @param bool $checked |
188
|
16 |
|
* |
189
|
|
|
* @return string |
190
|
16 |
|
*/ |
191
|
16 |
|
protected function checkbox($checked) |
192
|
|
|
{ |
193
|
|
|
if ($checked) { |
194
|
16 |
|
return html_entity_decode("●"); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return html_entity_decode("○"); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the pointer symbol |
202
|
16 |
|
* |
203
|
|
|
* @return string |
204
|
16 |
|
*/ |
205
|
|
|
protected function pointer() |
206
|
|
|
{ |
207
|
16 |
|
return html_entity_decode("❯"); |
208
|
|
|
} |
209
|
16 |
|
|
210
|
16 |
|
public function __toString() |
211
|
|
|
{ |
212
|
|
|
if ($this->isFirst()) { |
213
|
16 |
|
return $this->buildCheckboxString(); |
214
|
16 |
|
} |
215
|
|
|
|
216
|
|
|
if ($this->isLast()) { |
217
|
16 |
|
return $this->buildCheckboxString() . $this->util->cursor->left(10) . '<hidden>'; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $this->buildCheckboxString(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|