CheckboxItem::showsItemExtra()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\CliMenu;
6
use PhpSchool\CliMenu\MenuStyle;
7
use PhpSchool\CliMenu\Style\CheckboxStyle;
8
use PhpSchool\CliMenu\Style\ItemStyle;
9
10
class CheckboxItem implements MenuItemInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $text;
16
17
    /**
18
     * @var callable
19
     */
20
    private $selectAction;
21
22
    /**
23
     * @var bool
24
     */
25
    private $showItemExtra;
26
27
    /**
28
     * @var bool
29
     */
30
    private $disabled;
31
32
    /**
33
     * The current checkbox state
34
     *
35
     * @var bool
36
     */
37
    private $checked = false;
38
39
    /**
40
     * @var CheckboxStyle
41
     */
42
    private $style;
43
44
    public function __construct(
45
        string $text,
46
        callable $selectAction,
47
        bool $showItemExtra = false,
48
        bool $disabled = false
49
    ) {
50
        $this->text = $text;
51
        $this->selectAction = $selectAction;
52
        $this->showItemExtra = $showItemExtra;
53
        $this->disabled = $disabled;
54
55
        $this->style = new CheckboxStyle();
56
    }
57
58
    /**
59
     * The output text for the item
60
     */
61
    public function getRows(MenuStyle $style, bool $selected = false) : array
62
    {
63
        return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
64
    }
65
66
    /**
67
     * Return the raw string of text
68
     */
69
    public function getText() : string
70
    {
71
        return $this->text;
72
    }
73
74
    /**
75
     * Set the raw string of text
76
     */
77
    public function setText(string $text) : void
78
    {
79
        $this->text = $text;
80
    }
81
82
    /**
83
     * Execute the items callable if required
84
     */
85
    public function getSelectAction() : ?callable
86
    {
87
        return function (CliMenu $cliMenu) {
88
            $this->toggle();
89
            $cliMenu->redraw();
90
91
            return ($this->selectAction)($cliMenu);
92
        };
93
    }
94
95
    /**
96
     * Can the item be selected
97
     */
98
    public function canSelect() : bool
99
    {
100
        return !$this->disabled;
101
    }
102
103
    /**
104
     * Whether or not we are showing item extra
105
     */
106
    public function showsItemExtra() : bool
107
    {
108
        return $this->showItemExtra;
109
    }
110
111
    /**
112
     * Enable showing item extra
113
     */
114
    public function showItemExtra() : void
115
    {
116
        $this->showItemExtra = true;
117
    }
118
119
    /**
120
     * Disable showing item extra
121
     */
122
    public function hideItemExtra() : void
123
    {
124
        $this->showItemExtra = false;
125
    }
126
127
    /**
128
     * Whether or not the item is checked
129
     */
130
    public function getChecked() : bool
131
    {
132
        return $this->checked;
133
    }
134
135
    /**
136
     * Sets checked state to true
137
     */
138
    public function setChecked() : void
139
    {
140
        $this->checked = true;
141
    }
142
143
    /**
144
     * Sets checked state to false
145
     */
146
    public function setUnchecked() : void
147
    {
148
        $this->checked = false;
149
    }
150
151
    /**
152
     * Toggles checked state
153
     */
154
    public function toggle() : void
155
    {
156
        $this->checked = !$this->checked;
157
    }
158
159
    /**
160
     * @return CheckboxStyle
161
     */
162
    public function getStyle() : ItemStyle
163
    {
164
        return $this->style;
165
    }
166
167
    public function setStyle(CheckboxStyle $style) : void
168
    {
169
        $this->style = $style;
170
    }
171
}
172