1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
6
|
|
|
|
7
|
|
|
class RadioItem 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
|
|
|
$parentItem = $cliMenu->getItemByIndex($cliMenu->getSelectedItemIndex()); |
50
|
|
|
|
51
|
|
|
$siblings = $parentItem instanceof SplitItem |
52
|
|
|
? $parentItem->getItems() |
53
|
|
|
: $cliMenu->getItems(); |
54
|
|
|
|
55
|
|
|
array_walk( |
56
|
|
|
array_filter( |
|
|
|
|
57
|
|
|
$siblings, |
58
|
|
|
function (MenuItemInterface $item) { |
59
|
|
|
return $item instanceof self; |
60
|
|
|
} |
61
|
|
|
), |
62
|
|
|
function (RadioItem $checkableItem) { |
63
|
|
|
$checkableItem->setUnchecked(); |
64
|
|
|
} |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$this->setChecked(); |
68
|
|
|
$cliMenu->redraw(); |
69
|
|
|
|
70
|
|
|
return ($this->selectAction)($cliMenu); |
71
|
|
|
}; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return the raw string of text |
76
|
|
|
*/ |
77
|
|
|
public function getText() : string |
78
|
|
|
{ |
79
|
|
|
return $this->text; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the raw string of text |
84
|
|
|
*/ |
85
|
|
|
public function setText(string $text) : void |
86
|
|
|
{ |
87
|
|
|
$this->text = $text; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Can the item be selected |
92
|
|
|
*/ |
93
|
|
|
public function canSelect() : bool |
94
|
|
|
{ |
95
|
|
|
return !$this->disabled; |
96
|
|
|
} |
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
|
|
|
|