1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Style; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\MenuItem\MenuItemInterface; |
6
|
|
|
|
7
|
|
|
class SelectableStyle implements ItemStyle |
8
|
|
|
{ |
9
|
|
|
private const DEFAULT_STYLES = [ |
10
|
|
|
'selectedMarker' => '● ', |
11
|
|
|
'unselectedMarker' => '○ ', |
12
|
|
|
'itemExtra' => '✔', |
13
|
|
|
'displaysExtra' => false, |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $selectedMarker; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $unselectedMarker; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $itemExtra; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $displaysExtra; |
35
|
|
|
|
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->selectedMarker = self::DEFAULT_STYLES['selectedMarker']; |
39
|
|
|
$this->unselectedMarker = self::DEFAULT_STYLES['unselectedMarker']; |
40
|
|
|
$this->itemExtra = self::DEFAULT_STYLES['itemExtra']; |
41
|
|
|
$this->displaysExtra = self::DEFAULT_STYLES['displaysExtra']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function hasChangedFromDefaults() : bool |
45
|
|
|
{ |
46
|
|
|
$currentValues = [ |
47
|
|
|
$this->selectedMarker, |
48
|
|
|
$this->unselectedMarker, |
49
|
|
|
$this->itemExtra, |
50
|
|
|
$this->displaysExtra, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
return $currentValues !== array_values(self::DEFAULT_STYLES); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getMarker(MenuItemInterface $item, bool $selected) : string |
57
|
|
|
{ |
58
|
|
|
return $selected ? $this->selectedMarker : $this->unselectedMarker; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getSelectedMarker() : string |
62
|
|
|
{ |
63
|
|
|
return $this->selectedMarker; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setSelectedMarker(string $marker) : self |
67
|
|
|
{ |
68
|
|
|
$this->selectedMarker = $marker; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getUnselectedMarker() : string |
74
|
|
|
{ |
75
|
|
|
return $this->unselectedMarker; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function setUnselectedMarker(string $marker) : self |
79
|
|
|
{ |
80
|
|
|
$this->unselectedMarker = $marker; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getItemExtra() : string |
86
|
|
|
{ |
87
|
|
|
return $this->itemExtra; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setItemExtra(string $itemExtra) : self |
91
|
|
|
{ |
92
|
|
|
$this->itemExtra = $itemExtra; |
93
|
|
|
|
94
|
|
|
// if we customise item extra, it means we most likely want to display it |
95
|
|
|
$this->setDisplaysExtra(true); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getDisplaysExtra() : bool |
101
|
|
|
{ |
102
|
|
|
return $this->displaysExtra; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setDisplaysExtra(bool $displaysExtra) : self |
106
|
|
|
{ |
107
|
|
|
$this->displaysExtra = $displaysExtra; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|