1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Builder; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
6
|
|
|
use PhpSchool\CliMenu\MenuItem\CheckableItem; |
7
|
|
|
use PhpSchool\CliMenu\MenuItem\LineBreakItem; |
8
|
|
|
use PhpSchool\CliMenu\MenuItem\MenuMenuItem; |
9
|
|
|
use PhpSchool\CliMenu\MenuItem\SelectableItem; |
10
|
|
|
use PhpSchool\CliMenu\MenuItem\SplitItem; |
11
|
|
|
use PhpSchool\CliMenu\MenuItem\StaticItem; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Aydin Hassan <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class SplitItemBuilder |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var CliMenu |
20
|
|
|
*/ |
21
|
|
|
private $menu; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SplitItem |
25
|
|
|
*/ |
26
|
|
|
private $splitItem; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Whether or not to auto create keyboard shortcuts for items |
30
|
|
|
* when they contain square brackets. Eg: [M]y item |
31
|
|
|
* |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $autoShortcuts = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Regex to auto match for shortcuts defaults to looking |
38
|
|
|
* for a single character encased in square brackets |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $autoShortcutsRegex = '/\[(.)\]/'; |
43
|
|
|
|
44
|
|
|
public function __construct(CliMenu $menu) |
45
|
|
|
{ |
46
|
|
|
$this->menu = $menu; |
47
|
|
|
$this->splitItem = new SplitItem(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function addItem( |
51
|
|
|
string $text, |
52
|
|
|
callable $itemCallable, |
53
|
|
|
bool $showItemExtra = false, |
54
|
|
|
bool $disabled = false |
55
|
|
|
) : self { |
56
|
|
|
$this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function addCheckableItem( |
62
|
|
|
string $text, |
63
|
|
|
callable $itemCallable, |
64
|
|
|
bool $showItemExtra = false, |
65
|
|
|
bool $disabled = false |
66
|
|
|
) : self { |
67
|
|
|
$this->splitItem->addItem(new CheckableItem($text, $itemCallable, $showItemExtra, $disabled)); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function addStaticItem(string $text) : self |
73
|
|
|
{ |
74
|
|
|
$this->splitItem->addItem(new StaticItem($text)); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
80
|
|
|
{ |
81
|
|
|
$this->splitItem->addItem(new LineBreakItem($breakChar, $lines)); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function addSubMenu(string $text, \Closure $callback) : self |
87
|
|
|
{ |
88
|
|
|
$builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal()); |
89
|
|
|
|
90
|
|
|
if ($this->autoShortcuts) { |
91
|
|
|
$builder->enableAutoShortcuts($this->autoShortcutsRegex); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$callback = $callback->bindTo($builder); |
95
|
|
|
$callback($builder); |
96
|
|
|
|
97
|
|
|
$menu = $builder->build(); |
98
|
|
|
$menu->setParent($this->menu); |
99
|
|
|
|
100
|
|
|
$this->splitItem->addItem(new MenuMenuItem( |
101
|
|
|
$text, |
102
|
|
|
$menu, |
103
|
|
|
$builder->isMenuDisabled() |
104
|
|
|
)); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setGutter(int $gutter) : self |
110
|
|
|
{ |
111
|
|
|
$this->splitItem->setGutter($gutter); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function enableAutoShortcuts(string $regex = null) : self |
117
|
|
|
{ |
118
|
|
|
$this->autoShortcuts = true; |
119
|
|
|
|
120
|
|
|
if (null !== $regex) { |
121
|
|
|
$this->autoShortcutsRegex = $regex; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function build() : SplitItem |
128
|
|
|
{ |
129
|
|
|
return $this->splitItem; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|