1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
6
|
|
|
use PhpSchool\CliMenu\Util\StringUtil; |
7
|
|
|
|
8
|
|
|
trait ToggableTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var bool |
12
|
|
|
*/ |
13
|
|
|
private $checked = false; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The output text for the item |
17
|
|
|
* |
18
|
|
|
* @param MenuStyle $style |
19
|
|
|
* @param bool $selected Currently unused in this class |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public function getRows(MenuStyle $style, bool $selected = false) : array |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$markerTypes = [ |
25
|
|
|
true => $this instanceof CheckableItem |
26
|
|
|
? $style->getCheckedMarker() |
27
|
|
|
: $style->getRadioMarker(), |
28
|
|
|
false => $this instanceof CheckableItem |
29
|
|
|
? $style->getUncheckedMarker() |
30
|
|
|
: $style->getUnradioMarker(), |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
$marker = sprintf("%s", $markerTypes[$this->checked]); |
34
|
|
|
|
35
|
|
|
$length = $style->getDisplaysExtra() |
36
|
|
|
? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2) |
37
|
|
|
: $style->getContentWidth(); |
38
|
|
|
|
39
|
|
|
$rows = explode( |
40
|
|
|
"\n", |
41
|
|
|
StringUtil::wordwrap( |
42
|
|
|
sprintf('%s%s', $marker, $this->text), |
43
|
|
|
$length, |
44
|
|
|
sprintf("\n%s", str_repeat(' ', mb_strlen($marker))) |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
return array_map(function ($row, $key) use ($style, $length) { |
49
|
|
|
$text = $this->disabled ? $style->getDisabledItemText($row) : $row; |
50
|
|
|
|
51
|
|
|
if ($key === 0) { |
52
|
|
|
return $this->showItemExtra |
53
|
|
|
? sprintf('%s%s %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra()) |
54
|
|
|
: $text; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $text; |
58
|
|
|
}, $rows, array_keys($rows)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Toggles checked state |
63
|
|
|
*/ |
64
|
|
|
public function toggle() : void |
65
|
|
|
{ |
66
|
|
|
$this->checked = !$this->checked; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets checked state to true |
71
|
|
|
*/ |
72
|
|
|
public function setChecked() : void |
73
|
|
|
{ |
74
|
|
|
$this->checked = true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets checked state to false |
79
|
|
|
*/ |
80
|
|
|
public function setUnchecked() : void |
81
|
|
|
{ |
82
|
|
|
$this->checked = false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getChecked() : bool |
86
|
|
|
{ |
87
|
|
|
return $this->checked; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.