1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\MenuItem; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
6
|
|
|
use PhpSchool\CliMenu\Style\ItemStyleInterface; |
|
|
|
|
7
|
|
|
use PhpSchool\CliMenu\Util\StringUtil; |
8
|
|
|
|
9
|
|
|
trait ToggableTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var callable |
13
|
|
|
*/ |
14
|
|
|
private $selectAction; |
15
|
|
|
|
16
|
|
|
private $text = ''; |
17
|
|
|
|
18
|
|
|
private $showItemExtra = false; |
19
|
|
|
|
20
|
|
|
private $disabled = false; |
21
|
|
|
|
22
|
|
|
private $checked = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ItemStyleInterface; |
26
|
|
|
*/ |
27
|
|
|
private $style; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The output text for the item |
31
|
|
|
*/ |
32
|
|
|
public function getRows(MenuStyle $style, bool $selected = false) : array |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$marker = sprintf("%s", $this->style->getMarker($this->checked)); |
35
|
|
|
|
36
|
|
|
$itemExtra = $this->style->getItemExtra(); |
37
|
|
|
|
38
|
|
|
$length = $this->style->getDisplaysExtra() |
39
|
|
|
? $style->getContentWidth() - (mb_strlen($itemExtra) + 2) |
40
|
|
|
: $style->getContentWidth(); |
41
|
|
|
|
42
|
|
|
$rows = explode( |
43
|
|
|
"\n", |
44
|
|
|
StringUtil::wordwrap( |
45
|
|
|
sprintf('%s%s', $marker, $this->text), |
46
|
|
|
$length, |
47
|
|
|
sprintf("\n%s", str_repeat(' ', mb_strlen($marker))) |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
return array_map(function ($row, $key) use ($style, $length, $itemExtra) { |
52
|
|
|
$text = $this->disabled ? $style->getDisabledItemText($row) : $row; |
53
|
|
|
|
54
|
|
|
if ($key === 0) { |
55
|
|
|
return $this->showItemExtra |
56
|
|
|
? sprintf('%s%s %s', $text, str_repeat(' ', $length - mb_strlen($row)), $itemExtra) |
57
|
|
|
: $text; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $text; |
61
|
|
|
}, $rows, array_keys($rows)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Toggles checked state |
66
|
|
|
*/ |
67
|
|
|
public function toggle() : void |
68
|
|
|
{ |
69
|
|
|
$this->checked = !$this->checked; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets checked state to true |
74
|
|
|
*/ |
75
|
|
|
public function setChecked() : void |
76
|
|
|
{ |
77
|
|
|
$this->checked = true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Sets checked state to false |
82
|
|
|
*/ |
83
|
|
|
public function setUnchecked() : void |
84
|
|
|
{ |
85
|
|
|
$this->checked = false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Whether or not the item is checked |
90
|
|
|
*/ |
91
|
|
|
public function getChecked() : bool |
92
|
|
|
{ |
93
|
|
|
return $this->checked; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return the raw string of text |
98
|
|
|
*/ |
99
|
|
|
public function getText() : string |
100
|
|
|
{ |
101
|
|
|
return $this->text; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the raw string of text |
106
|
|
|
*/ |
107
|
|
|
public function setText(string $text) : void |
108
|
|
|
{ |
109
|
|
|
$this->text = $text; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Can the item be selected |
114
|
|
|
*/ |
115
|
|
|
public function canSelect() : bool |
116
|
|
|
{ |
117
|
|
|
return !$this->disabled; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function showsItemExtra() : bool |
121
|
|
|
{ |
122
|
|
|
return $this->showItemExtra; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Enable showing item extra |
127
|
|
|
*/ |
128
|
|
|
public function showItemExtra() : void |
129
|
|
|
{ |
130
|
|
|
$this->showItemExtra = true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Disable showing item extra |
135
|
|
|
*/ |
136
|
|
|
public function hideItemExtra() : void |
137
|
|
|
{ |
138
|
|
|
$this->showItemExtra = false; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: