|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
|
6
|
|
|
|
|
7
|
|
|
class CheckboxItem implements MenuItemInterface, ToggableItemInterface |
|
8
|
|
|
{ |
|
9
|
|
|
use ToggableTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var callable |
|
13
|
|
|
*/ |
|
14
|
|
|
private $selectAction; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $text = ''; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private $showItemExtra = false; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
private $disabled = false; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
string $text, |
|
33
|
|
|
callable $selectAction, |
|
34
|
|
|
bool $showItemExtra = false, |
|
35
|
|
|
bool $disabled = false |
|
36
|
|
|
) { |
|
37
|
|
|
$this->text = $text; |
|
38
|
|
|
$this->selectAction = $selectAction; |
|
39
|
|
|
$this->showItemExtra = $showItemExtra; |
|
40
|
|
|
$this->disabled = $disabled; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Execute the items callable if required |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getSelectAction() : ?callable |
|
47
|
|
|
{ |
|
48
|
|
|
return function (CliMenu $cliMenu) { |
|
49
|
|
|
$this->toggle(); |
|
50
|
|
|
$cliMenu->redraw(); |
|
51
|
|
|
|
|
52
|
|
|
return ($this->selectAction)($cliMenu); |
|
53
|
|
|
}; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Return the raw string of text |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getText() : string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->text; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set the raw string of text |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setText(string $text) : void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->text = $text; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Can the item be selected |
|
74
|
|
|
*/ |
|
75
|
|
|
public function canSelect() : bool |
|
76
|
|
|
{ |
|
77
|
|
|
return !$this->disabled; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function showsItemExtra() : bool |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->showItemExtra; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Enable showing item extra |
|
87
|
|
|
*/ |
|
88
|
|
|
public function showItemExtra() : void |
|
89
|
|
|
{ |
|
90
|
|
|
$this->showItemExtra = true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Disable showing item extra |
|
95
|
|
|
*/ |
|
96
|
|
|
public function hideItemExtra() : void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->showItemExtra = false; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|