1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Style; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\Terminal\TerminalFactory; |
6
|
|
|
use PhpSchool\Terminal\Terminal; |
7
|
|
|
|
8
|
|
|
class CheckableStyle implements ItemStyleInterface |
9
|
|
|
{ |
10
|
|
|
use ItemStyleTrait; |
11
|
|
|
|
12
|
|
|
protected const DEFAULT_STYLES = [ |
13
|
|
|
'fg' => 'white', |
14
|
|
|
'bg' => 'blue', |
15
|
|
|
'markerOn' => '[✔] ', |
16
|
|
|
'markerOff' => '[ ] ', |
17
|
|
|
'itemExtra' => '✔', |
18
|
|
|
'displaysExtra' => false, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
public function __construct(Terminal $terminal = null) |
22
|
|
|
{ |
23
|
|
|
$this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
24
|
|
|
|
25
|
|
|
$this->fg = self::DEFAULT_STYLES['fg']; |
26
|
|
|
$this->bg = self::DEFAULT_STYLES['bg']; |
27
|
|
|
|
28
|
|
|
$this->generateColoursSetCode(); |
29
|
|
|
|
30
|
|
|
$this->setMarkerOn(self::DEFAULT_STYLES['markerOn']); |
31
|
|
|
$this->setMarkerOff(self::DEFAULT_STYLES['markerOff']); |
32
|
|
|
$this->setItemExtra(self::DEFAULT_STYLES['itemExtra']); |
33
|
|
|
$this->setDisplaysExtra(self::DEFAULT_STYLES['displaysExtra']); |
34
|
|
|
|
35
|
|
|
$this->custom = false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function toArray(): array |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
'fg' => $this->fg, |
42
|
|
|
'bg' => $this->bg, |
43
|
|
|
'markerOn' => $this->markerOn, |
44
|
|
|
'markerOff' => $this->markerOff, |
45
|
|
|
'itemExtra' => $this->itemExtra, |
46
|
|
|
'displaysExtra' => $this->displaysExtra, |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function fromArray(array $style) : self |
51
|
|
|
{ |
52
|
|
|
$this->fg = $style['fg'] ?? $this->fg; |
53
|
|
|
$this->bg = $style['bg'] ?? $this->bg; |
54
|
|
|
$this->markerOn = $style['markerOn'] ?? $this->markerOn; |
55
|
|
|
$this->markerOff = $style['markerOff'] ?? $this->markerOff; |
56
|
|
|
$this->itemExtra = $style['itemExtra'] ?? $this->itemExtra; |
57
|
|
|
$this->displaysExtra = $style['displaysExtra'] ?? $this->displaysExtra; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|