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
|
|
|
$length = $style->getDisplaysExtra() |
37
|
|
|
? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2) |
38
|
|
|
: $style->getContentWidth(); |
39
|
|
|
|
40
|
|
|
$rows = explode( |
41
|
|
|
"\n", |
42
|
|
|
StringUtil::wordwrap( |
43
|
|
|
sprintf('%s%s', $marker, $this->text), |
44
|
|
|
$length, |
45
|
|
|
sprintf("\n%s", str_repeat(' ', mb_strlen($marker))) |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
return array_map(function ($row, $key) use ($style, $length) { |
50
|
|
|
$text = $this->disabled ? $style->getDisabledItemText($row) : $row; |
51
|
|
|
|
52
|
|
|
if ($key === 0) { |
53
|
|
|
return $this->showItemExtra |
54
|
|
|
? sprintf('%s%s %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra()) |
55
|
|
|
: $text; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $text; |
59
|
|
|
}, $rows, array_keys($rows)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Toggles checked state |
64
|
|
|
*/ |
65
|
|
|
public function toggle() : void |
66
|
|
|
{ |
67
|
|
|
$this->checked = !$this->checked; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sets checked state to true |
72
|
|
|
*/ |
73
|
|
|
public function setChecked() : void |
74
|
|
|
{ |
75
|
|
|
$this->checked = true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets checked state to false |
80
|
|
|
*/ |
81
|
|
|
public function setUnchecked() : void |
82
|
|
|
{ |
83
|
|
|
$this->checked = false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Whether or not the item is checked |
88
|
|
|
*/ |
89
|
|
|
public function getChecked() : bool |
90
|
|
|
{ |
91
|
|
|
return $this->checked; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return the raw string of text |
96
|
|
|
*/ |
97
|
|
|
public function getText() : string |
98
|
|
|
{ |
99
|
|
|
return $this->text; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the raw string of text |
104
|
|
|
*/ |
105
|
|
|
public function setText(string $text) : void |
106
|
|
|
{ |
107
|
|
|
$this->text = $text; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Can the item be selected |
112
|
|
|
*/ |
113
|
|
|
public function canSelect() : bool |
114
|
|
|
{ |
115
|
|
|
return !$this->disabled; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function showsItemExtra() : bool |
119
|
|
|
{ |
120
|
|
|
return $this->showItemExtra; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Enable showing item extra |
125
|
|
|
*/ |
126
|
|
|
public function showItemExtra() : void |
127
|
|
|
{ |
128
|
|
|
$this->showItemExtra = true; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Disable showing item extra |
133
|
|
|
*/ |
134
|
|
|
public function hideItemExtra() : void |
135
|
|
|
{ |
136
|
|
|
$this->showItemExtra = false; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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: