1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
6
|
|
|
use PhpSchool\CliMenu\Style\ItemStyle; |
7
|
|
|
use PhpSchool\CliMenu\Util\StringUtil; |
8
|
|
|
use PhpSchool\CliMenu\Style\SelectableStyle; |
9
|
|
|
use function PhpSchool\CliMenu\Util\mapWithKeys; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Michael Woodward <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class SelectableItem implements MenuItemInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $text; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var callable |
23
|
|
|
*/ |
24
|
|
|
private $selectAction; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $showItemExtra; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $disabled; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var SelectableStyle |
38
|
|
|
*/ |
39
|
|
|
private $style; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
string $text, |
43
|
|
|
callable $selectAction, |
44
|
|
|
bool $showItemExtra = false, |
45
|
|
|
bool $disabled = false |
46
|
|
|
) { |
47
|
|
|
$this->text = $text; |
48
|
|
|
$this->selectAction = $selectAction; |
49
|
|
|
$this->showItemExtra = $showItemExtra; |
50
|
|
|
$this->disabled = $disabled; |
51
|
|
|
|
52
|
|
|
$this->style = new SelectableStyle(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The output text for the item |
57
|
|
|
*/ |
58
|
|
|
public function getRows(MenuStyle $style, bool $selected = false) : array |
59
|
|
|
{ |
60
|
|
|
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the raw string of text |
65
|
|
|
*/ |
66
|
|
|
public function getText() : string |
67
|
|
|
{ |
68
|
|
|
return $this->text; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the raw string of text |
73
|
|
|
*/ |
74
|
|
|
public function setText(string $text) : void |
75
|
|
|
{ |
76
|
|
|
$this->text = $text; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Execute the items callable if required |
81
|
|
|
*/ |
82
|
|
|
public function getSelectAction() : ?callable |
83
|
|
|
{ |
84
|
|
|
return $this->selectAction; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Can the item be selected |
89
|
|
|
*/ |
90
|
|
|
public function canSelect() : bool |
91
|
|
|
{ |
92
|
|
|
return !$this->disabled; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Whether or not we are showing item extra |
97
|
|
|
*/ |
98
|
|
|
public function showsItemExtra() : bool |
99
|
|
|
{ |
100
|
|
|
return $this->showItemExtra; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Enable showing item extra |
105
|
|
|
*/ |
106
|
|
|
public function showItemExtra() : void |
107
|
|
|
{ |
108
|
|
|
$this->showItemExtra = true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Disable showing item extra |
113
|
|
|
*/ |
114
|
|
|
public function hideItemExtra() : void |
115
|
|
|
{ |
116
|
|
|
$this->showItemExtra = false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return SelectableStyle |
121
|
|
|
*/ |
122
|
|
|
public function getStyle() : ItemStyle |
123
|
|
|
{ |
124
|
|
|
return $this->style; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function setStyle(SelectableStyle $style) : void |
128
|
|
|
{ |
129
|
|
|
$this->style = $style; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|