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\RadioStyle; |
9
|
|
|
|
10
|
|
|
class RadioItem 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 RadioStyle; |
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 RadioStyle(); |
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
|
|
|
$parentItem = $cliMenu->getItemByIndex($cliMenu->getSelectedItemIndex()); |
86
|
|
|
|
87
|
|
|
$siblings = $parentItem instanceof SplitItem |
88
|
|
|
? $parentItem->getItems() |
89
|
|
|
: $cliMenu->getItems(); |
90
|
|
|
|
91
|
|
|
$filtered = array_filter( |
92
|
|
|
$siblings, |
93
|
|
|
function (MenuItemInterface $item) { |
94
|
|
|
return $item instanceof self; |
95
|
|
|
} |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
array_walk( |
99
|
|
|
$filtered, |
100
|
|
|
function (RadioItem $item) { |
101
|
|
|
$item->setUnchecked(); |
102
|
|
|
} |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->setChecked(); |
106
|
|
|
$cliMenu->redraw(); |
107
|
|
|
|
108
|
|
|
return ($this->selectAction)($cliMenu); |
109
|
|
|
}; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getStyle() : RadioStyle |
113
|
|
|
{ |
114
|
|
|
return $this->style; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function setStyle(RadioStyle $style) : self |
118
|
|
|
{ |
119
|
|
|
$this->style = $style; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Toggles checked state |
126
|
|
|
*/ |
127
|
|
|
public function toggle() : void |
128
|
|
|
{ |
129
|
|
|
$this->checked = !$this->checked; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets checked state to true |
134
|
|
|
*/ |
135
|
|
|
public function setChecked() : void |
136
|
|
|
{ |
137
|
|
|
$this->checked = true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets checked state to false |
142
|
|
|
*/ |
143
|
|
|
public function setUnchecked() : void |
144
|
|
|
{ |
145
|
|
|
$this->checked = false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Whether or not the item is checked |
150
|
|
|
*/ |
151
|
|
|
public function getChecked() : bool |
152
|
|
|
{ |
153
|
|
|
return $this->checked; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return the raw string of text |
158
|
|
|
*/ |
159
|
|
|
public function getText() : string |
160
|
|
|
{ |
161
|
|
|
return $this->text; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set the raw string of text |
166
|
|
|
*/ |
167
|
|
|
public function setText(string $text) : void |
168
|
|
|
{ |
169
|
|
|
$this->text = $text; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Can the item be selected |
174
|
|
|
*/ |
175
|
|
|
public function canSelect() : bool |
176
|
|
|
{ |
177
|
|
|
return !$this->disabled; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function showsItemExtra() : bool |
181
|
|
|
{ |
182
|
|
|
return $this->showItemExtra; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Enable showing item extra |
187
|
|
|
*/ |
188
|
|
|
public function showItemExtra() : void |
189
|
|
|
{ |
190
|
|
|
$this->showItemExtra = true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Disable showing item extra |
195
|
|
|
*/ |
196
|
|
|
public function hideItemExtra() : void |
197
|
|
|
{ |
198
|
|
|
$this->showItemExtra = false; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|