RadioStyle::setUncheckedMarker()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpSchool\CliMenu\Style;
4
5
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;
6
use PhpSchool\CliMenu\MenuItem\RadioItem;
7
8
class RadioStyle implements ItemStyle
9
{
10
    private const DEFAULT_STYLES = [
11
        'checkedMarker' => '[●] ',
12
        'uncheckedMarker' => '[○] ',
13
        'itemExtra' => '✔',
14
        'displaysExtra' => false,
15
    ];
16
17
    /**
18
     * @var string
19
     */
20
    private $checkedMarker;
21
22
    /**
23
     * @var string
24
     */
25
    private $uncheckedMarker;
26
27
    /**
28
     * @var string
29
     */
30
    private $itemExtra;
31
32
    /**
33
     * @var bool
34
     */
35
    private $displaysExtra;
36
37
    public function __construct()
38
    {
39
        $this->checkedMarker = self::DEFAULT_STYLES['checkedMarker'];
40
        $this->uncheckedMarker = self::DEFAULT_STYLES['uncheckedMarker'];
41
        $this->itemExtra = self::DEFAULT_STYLES['itemExtra'];
42
        $this->displaysExtra = self::DEFAULT_STYLES['displaysExtra'];
43
    }
44
45
    public function hasChangedFromDefaults() : bool
46
    {
47
        $currentValues = [
48
            $this->checkedMarker,
49
            $this->uncheckedMarker,
50
            $this->itemExtra,
51
            $this->displaysExtra,
52
        ];
53
54
        return $currentValues !== array_values(self::DEFAULT_STYLES);
55
    }
56
57
    public function getMarker(MenuItemInterface $item, bool $selected) : string
58
    {
59
        if (!$item instanceof RadioItem) {
60
            throw new \InvalidArgumentException(
61
                sprintf('Expected an instance of: %s. Got: %s', RadioItem::class, get_class($item))
62
            );
63
        }
64
65
        return $item->getChecked() ? $this->checkedMarker : $this->uncheckedMarker;
66
    }
67
68
    public function getCheckedMarker() : string
69
    {
70
        return $this->checkedMarker;
71
    }
72
73
    public function setCheckedMarker(string $marker) : self
74
    {
75
        $this->checkedMarker = $marker;
76
77
        return $this;
78
    }
79
80
    public function getUncheckedMarker() : string
81
    {
82
        return $this->uncheckedMarker;
83
    }
84
85
    public function setUncheckedMarker(string $marker) : self
86
    {
87
        $this->uncheckedMarker = $marker;
88
89
        return $this;
90
    }
91
92
    public function getItemExtra() : string
93
    {
94
        return $this->itemExtra;
95
    }
96
97
    public function setItemExtra(string $itemExtra) : self
98
    {
99
        $this->itemExtra = $itemExtra;
100
101
        // if we customise item extra, it means we most likely want to display it
102
        $this->setDisplaysExtra(true);
103
104
        return $this;
105
    }
106
107
    public function getDisplaysExtra() : bool
108
    {
109
        return $this->displaysExtra;
110
    }
111
112
    public function setDisplaysExtra(bool $displaysExtra) : self
113
    {
114
        $this->displaysExtra = $displaysExtra;
115
116
        return $this;
117
    }
118
}
119