1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
6
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
7
|
|
|
use PhpSchool\CliMenu\Util\StringUtil; |
8
|
|
|
use PhpSchool\CliMenu\Style\CheckboxStyle; |
9
|
|
|
|
10
|
|
|
class CheckboxItem implements MenuItemInterface, ToggableItemInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var callable |
14
|
|
|
*/ |
15
|
|
|
private $selectAction; |
16
|
|
|
|
17
|
|
|
private $text = ''; |
18
|
|
|
|
19
|
|
|
private $showItemExtra = false; |
20
|
|
|
|
21
|
|
|
private $disabled = false; |
22
|
|
|
|
23
|
|
|
private $checked = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CheckboxStyle; |
27
|
|
|
*/ |
28
|
|
|
private $style; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
string $text, |
32
|
|
|
callable $selectAction, |
33
|
|
|
bool $showItemExtra = false, |
34
|
|
|
bool $disabled = false |
35
|
|
|
) { |
36
|
|
|
$this->text = $text; |
37
|
|
|
$this->selectAction = $selectAction; |
38
|
|
|
$this->showItemExtra = $showItemExtra; |
39
|
|
|
$this->disabled = $disabled; |
40
|
|
|
|
41
|
|
|
$this->style = new CheckboxStyle(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The output text for the item |
46
|
|
|
*/ |
47
|
|
|
public function getRows(MenuStyle $style, bool $selected = false) : array |
48
|
|
|
{ |
49
|
|
|
$marker = sprintf("%s", $this->style->getMarker($this->checked)); |
50
|
|
|
|
51
|
|
|
$itemExtra = $this->style->getItemExtra(); |
52
|
|
|
|
53
|
|
|
$length = $this->style->getDisplaysExtra() |
54
|
|
|
? $style->getContentWidth() - (mb_strlen($itemExtra) + 2) |
55
|
|
|
: $style->getContentWidth(); |
56
|
|
|
|
57
|
|
|
$rows = explode( |
58
|
|
|
"\n", |
59
|
|
|
StringUtil::wordwrap( |
60
|
|
|
sprintf('%s%s', $marker, $this->text), |
61
|
|
|
$length, |
62
|
|
|
sprintf("\n%s", str_repeat(' ', mb_strlen($marker))) |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return array_map(function ($row, $key) use ($style, $length, $itemExtra) { |
67
|
|
|
$text = $this->disabled ? $style->getDisabledItemText($row) : $row; |
68
|
|
|
|
69
|
|
|
if ($key === 0) { |
70
|
|
|
return $this->showItemExtra |
71
|
|
|
? sprintf('%s%s %s', $text, str_repeat(' ', $length - mb_strlen($row)), $itemExtra) |
72
|
|
|
: $text; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $text; |
76
|
|
|
}, $rows, array_keys($rows)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Execute the items callable if required |
81
|
|
|
*/ |
82
|
|
|
public function getSelectAction() : ?callable |
83
|
|
|
{ |
84
|
|
|
return function (CliMenu $cliMenu) { |
85
|
|
|
$this->toggle(); |
86
|
|
|
$cliMenu->redraw(); |
87
|
|
|
|
88
|
|
|
return ($this->selectAction)($cliMenu); |
89
|
|
|
}; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getStyle() : CheckboxStyle |
93
|
|
|
{ |
94
|
|
|
return $this->style; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setStyle(CheckboxStyle $style) : self |
98
|
|
|
{ |
99
|
|
|
$this->style = $style; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Toggles checked state |
106
|
|
|
*/ |
107
|
|
|
public function toggle() : void |
108
|
|
|
{ |
109
|
|
|
$this->checked = !$this->checked; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sets checked state to true |
114
|
|
|
*/ |
115
|
|
|
public function setChecked() : void |
116
|
|
|
{ |
117
|
|
|
$this->checked = true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets checked state to false |
122
|
|
|
*/ |
123
|
|
|
public function setUnchecked() : void |
124
|
|
|
{ |
125
|
|
|
$this->checked = false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Whether or not the item is checked |
130
|
|
|
*/ |
131
|
|
|
public function getChecked() : bool |
132
|
|
|
{ |
133
|
|
|
return $this->checked; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return the raw string of text |
138
|
|
|
*/ |
139
|
|
|
public function getText() : string |
140
|
|
|
{ |
141
|
|
|
return $this->text; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the raw string of text |
146
|
|
|
*/ |
147
|
|
|
public function setText(string $text) : void |
148
|
|
|
{ |
149
|
|
|
$this->text = $text; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Can the item be selected |
154
|
|
|
*/ |
155
|
|
|
public function canSelect() : bool |
156
|
|
|
{ |
157
|
|
|
return !$this->disabled; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function showsItemExtra() : bool |
161
|
|
|
{ |
162
|
|
|
return $this->showItemExtra; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Enable showing item extra |
167
|
|
|
*/ |
168
|
|
|
public function showItemExtra() : void |
169
|
|
|
{ |
170
|
|
|
$this->showItemExtra = true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Disable showing item extra |
175
|
|
|
*/ |
176
|
|
|
public function hideItemExtra() : void |
177
|
|
|
{ |
178
|
|
|
$this->showItemExtra = false; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|