1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Builder; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
6
|
|
|
use PhpSchool\CliMenu\MenuItem\CheckboxItem; |
7
|
|
|
use PhpSchool\CliMenu\MenuItem\LineBreakItem; |
8
|
|
|
use PhpSchool\CliMenu\MenuItem\MenuMenuItem; |
9
|
|
|
use PhpSchool\CliMenu\MenuItem\RadioItem; |
10
|
|
|
use PhpSchool\CliMenu\MenuItem\SelectableItem; |
11
|
|
|
use PhpSchool\CliMenu\MenuItem\SplitItem; |
12
|
|
|
use PhpSchool\CliMenu\MenuItem\StaticItem; |
13
|
|
|
use PhpSchool\CliMenu\Style\CheckboxStyle; |
14
|
|
|
use PhpSchool\CliMenu\Style\RadioStyle; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Aydin Hassan <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class SplitItemBuilder |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var CliMenu |
23
|
|
|
*/ |
24
|
|
|
private $menu; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var SplitItem |
28
|
|
|
*/ |
29
|
|
|
private $splitItem; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Whether or not to auto create keyboard shortcuts for items |
33
|
|
|
* when they contain square brackets. Eg: [M]y item |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $autoShortcuts = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Regex to auto match for shortcuts defaults to looking |
41
|
|
|
* for a single character encased in square brackets |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $autoShortcutsRegex = '/\[(.)\]/'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var CheckboxStyle |
49
|
|
|
*/ |
50
|
|
|
private $checkboxStyle; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var RadioStyle |
54
|
|
|
*/ |
55
|
|
|
private $radioStyle; |
56
|
|
|
|
57
|
|
|
public function __construct(CliMenu $menu) |
58
|
|
|
{ |
59
|
|
|
$this->menu = $menu; |
60
|
|
|
$this->splitItem = new SplitItem(); |
61
|
|
|
|
62
|
|
|
$this->checkboxStyle = new CheckboxStyle(); |
63
|
|
|
$this->radioStyle = new RadioStyle(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function addItem( |
67
|
|
|
string $text, |
68
|
|
|
callable $itemCallable, |
69
|
|
|
bool $showItemExtra = false, |
70
|
|
|
bool $disabled = false |
71
|
|
|
) : self { |
72
|
|
|
$this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function addCheckboxItem( |
78
|
|
|
string $text, |
79
|
|
|
callable $itemCallable, |
80
|
|
|
bool $showItemExtra = false, |
81
|
|
|
bool $disabled = false |
82
|
|
|
) : self { |
83
|
|
|
$item = (new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)) |
84
|
|
|
->setStyle($this->menu->getCheckboxStyle()); |
85
|
|
|
|
86
|
|
|
$this->splitItem->addItem($item); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function addRadioItem( |
92
|
|
|
string $text, |
93
|
|
|
callable $itemCallable, |
94
|
|
|
bool $showItemExtra = false, |
95
|
|
|
bool $disabled = false |
96
|
|
|
) : self { |
97
|
|
|
$item = (new RadioItem($text, $itemCallable, $showItemExtra, $disabled)) |
98
|
|
|
->setStyle($this->menu->getRadioStyle()); |
99
|
|
|
|
100
|
|
|
$this->splitItem->addItem($item); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function addStaticItem(string $text) : self |
106
|
|
|
{ |
107
|
|
|
$this->splitItem->addItem(new StaticItem($text)); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
113
|
|
|
{ |
114
|
|
|
$this->splitItem->addItem(new LineBreakItem($breakChar, $lines)); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function addSubMenu(string $text, \Closure $callback) : self |
120
|
|
|
{ |
121
|
|
|
$builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal()); |
122
|
|
|
|
123
|
|
|
if ($this->autoShortcuts) { |
124
|
|
|
$builder->enableAutoShortcuts($this->autoShortcutsRegex); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$callback($builder); |
128
|
|
|
|
129
|
|
|
$menu = $builder->build(); |
130
|
|
|
$menu->setParent($this->menu); |
131
|
|
|
|
132
|
|
|
$menu->checkboxStyle(function (CheckboxStyle $style) { |
133
|
|
|
$style->fromArray($this->menu->getCheckboxStyle()->toArray()); |
134
|
|
|
}); |
135
|
|
|
|
136
|
|
|
$menu->radioStyle(function (RadioStyle $style) { |
137
|
|
|
$style->fromArray($this->menu->getRadioStyle()->toArray()); |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
$this->splitItem->addItem(new MenuMenuItem( |
141
|
|
|
$text, |
142
|
|
|
$menu, |
143
|
|
|
$builder->isMenuDisabled() |
144
|
|
|
)); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function setGutter(int $gutter) : self |
150
|
|
|
{ |
151
|
|
|
$this->splitItem->setGutter($gutter); |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function enableAutoShortcuts(string $regex = null) : self |
157
|
|
|
{ |
158
|
|
|
$this->autoShortcuts = true; |
159
|
|
|
|
160
|
|
|
if (null !== $regex) { |
161
|
|
|
$this->autoShortcutsRegex = $regex; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function build() : SplitItem |
168
|
|
|
{ |
169
|
|
|
return $this->splitItem; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Use as |
174
|
|
|
* |
175
|
|
|
->checkboxStyle(function (CheckboxStyle $style) { |
176
|
|
|
$style->setMarkerOff('- '); |
177
|
|
|
}) |
178
|
|
|
* |
179
|
|
|
* @param callable $itemCallable |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function checkboxStyle(callable $itemCallable) : self |
183
|
|
|
{ |
184
|
|
|
$this->menu->checkboxStyle($itemCallable); |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Use as |
191
|
|
|
* |
192
|
|
|
->radioStyle(function (RadioStyle $style) { |
193
|
|
|
$style->setMarkerOff('- '); |
194
|
|
|
}) |
195
|
|
|
* |
196
|
|
|
* @param callable $itemCallable |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
public function radioStyle(callable $itemCallable) : self |
200
|
|
|
{ |
201
|
|
|
$this->menu->radioStyle($itemCallable); |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|