1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Builder; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
6
|
|
|
use PhpSchool\CliMenu\MenuItem\LineBreakItem; |
7
|
|
|
use PhpSchool\CliMenu\MenuItem\MenuMenuItem; |
8
|
|
|
use PhpSchool\CliMenu\MenuItem\SelectableItem; |
9
|
|
|
use PhpSchool\CliMenu\MenuItem\SplitItem; |
10
|
|
|
use PhpSchool\CliMenu\MenuItem\StaticItem; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Aydin Hassan <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class SplitItemBuilder |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CliMenu |
19
|
|
|
*/ |
20
|
|
|
private $menu; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var SplitItem |
24
|
|
|
*/ |
25
|
|
|
private $splitItem; |
26
|
|
|
|
27
|
|
|
public function __construct(CliMenu $menu) |
28
|
|
|
{ |
29
|
|
|
$this->menu = $menu; |
30
|
|
|
$this->splitItem = new SplitItem(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function addItem( |
34
|
|
|
string $text, |
35
|
|
|
callable $itemCallable, |
36
|
|
|
bool $showItemExtra = false, |
37
|
|
|
bool $disabled = false |
38
|
|
|
) : self { |
39
|
|
|
$this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function addStaticItem(string $text) : self |
45
|
|
|
{ |
46
|
|
|
$this->splitItem->addItem(new StaticItem($text)); |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
52
|
|
|
{ |
53
|
|
|
$this->splitItem->addItem(new LineBreakItem($breakChar, $lines)); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function addSubMenu(string $text, \Closure $callback) : self |
59
|
|
|
{ |
60
|
|
|
$builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal()); |
61
|
|
|
|
62
|
|
|
$callback = $callback->bindTo($builder); |
63
|
|
|
$callback($builder); |
64
|
|
|
|
65
|
|
|
$menu = $builder->build(); |
66
|
|
|
$menu->setParent($this->menu); |
67
|
|
|
|
68
|
|
|
$this->splitItem->addItem(new MenuMenuItem( |
69
|
|
|
$text, |
70
|
|
|
$menu, |
71
|
|
|
$builder->isMenuDisabled() |
72
|
|
|
)); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setGutter(int $gutter) : self |
78
|
|
|
{ |
79
|
|
|
$this->splitItem->setGutter($gutter); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function build() : SplitItem |
85
|
|
|
{ |
86
|
|
|
return $this->splitItem; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|