|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
|
6
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
|
7
|
|
|
use PhpSchool\CliMenu\Util\StringUtil; |
|
8
|
|
|
use PhpSchool\CliMenu\Style\RadioStyle; |
|
9
|
|
|
|
|
10
|
|
|
class RadioItem implements MenuItemInterface, ToggableItemInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use ToggableTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var RadioStyle; |
|
16
|
|
|
*/ |
|
17
|
|
|
private $style; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct( |
|
20
|
|
|
string $text, |
|
21
|
|
|
callable $selectAction, |
|
22
|
|
|
bool $showItemExtra = false, |
|
23
|
|
|
bool $disabled = false |
|
24
|
|
|
) { |
|
25
|
|
|
$this->text = $text; |
|
26
|
|
|
$this->selectAction = $selectAction; |
|
27
|
|
|
$this->showItemExtra = $showItemExtra; |
|
28
|
|
|
$this->disabled = $disabled; |
|
29
|
|
|
|
|
30
|
|
|
$this->style = new RadioStyle(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The output text for the item |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getRows(MenuStyle $style, bool $selected = false) : array |
|
37
|
|
|
{ |
|
38
|
|
|
$marker = sprintf("%s", $this->style->getMarker($this->checked)); |
|
39
|
|
|
|
|
40
|
|
|
$itemExtra = $this->style->getItemExtra(); |
|
41
|
|
|
|
|
42
|
|
|
$length = $this->style->getDisplaysExtra() |
|
43
|
|
|
? $style->getContentWidth() - (mb_strlen($itemExtra) + 2) |
|
44
|
|
|
: $style->getContentWidth(); |
|
45
|
|
|
|
|
46
|
|
|
$rows = explode( |
|
47
|
|
|
"\n", |
|
48
|
|
|
StringUtil::wordwrap( |
|
49
|
|
|
sprintf('%s%s', $marker, $this->text), |
|
50
|
|
|
$length, |
|
51
|
|
|
sprintf("\n%s", str_repeat(' ', mb_strlen($marker))) |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
return array_map(function ($row, $key) use ($style, $length, $itemExtra) { |
|
56
|
|
|
$text = $this->disabled ? $style->getDisabledItemText($row) : $row; |
|
57
|
|
|
|
|
58
|
|
|
if ($key === 0) { |
|
59
|
|
|
return $this->showItemExtra |
|
60
|
|
|
? sprintf('%s%s %s', $text, str_repeat(' ', $length - mb_strlen($row)), $itemExtra) |
|
61
|
|
|
: $text; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $text; |
|
65
|
|
|
}, $rows, array_keys($rows)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Execute the items callable if required |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getSelectAction() : ?callable |
|
72
|
|
|
{ |
|
73
|
|
|
return function (CliMenu $cliMenu) { |
|
74
|
|
|
$parentItem = $cliMenu->getItemByIndex($cliMenu->getSelectedItemIndex()); |
|
75
|
|
|
|
|
76
|
|
|
$siblings = $parentItem instanceof SplitItem |
|
77
|
|
|
? $parentItem->getItems() |
|
78
|
|
|
: $cliMenu->getItems(); |
|
79
|
|
|
|
|
80
|
|
|
$filtered = array_filter( |
|
81
|
|
|
$siblings, |
|
82
|
|
|
function (MenuItemInterface $item) { |
|
83
|
|
|
return $item instanceof self; |
|
84
|
|
|
} |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
array_walk( |
|
88
|
|
|
$filtered, |
|
89
|
|
|
function (RadioItem $item) { |
|
90
|
|
|
$item->setUnchecked(); |
|
91
|
|
|
} |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
$this->setChecked(); |
|
95
|
|
|
$cliMenu->redraw(); |
|
96
|
|
|
|
|
97
|
|
|
return ($this->selectAction)($cliMenu); |
|
98
|
|
|
}; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getStyle() : RadioStyle |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->style; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function setStyle(RadioStyle $style) : self |
|
107
|
|
|
{ |
|
108
|
|
|
$this->style = $style; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|