RadioItem   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 41
c 3
b 0
f 0
dl 0
loc 180
rs 10
wmc 16

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A canSelect() 0 3 1
A getStyle() 0 3 1
A getChecked() 0 3 1
A setUnchecked() 0 3 1
A showsItemExtra() 0 3 1
A setChecked() 0 3 1
A setText() 0 3 1
A hideItemExtra() 0 3 1
A toggle() 0 3 1
A showItemExtra() 0 3 1
A getText() 0 3 1
A setStyle() 0 3 1
A getRows() 0 3 1
A getSelectAction() 0 27 2
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\CliMenu;
6
use PhpSchool\CliMenu\MenuStyle;
7
use PhpSchool\CliMenu\Style\ItemStyle;
8
use PhpSchool\CliMenu\Style\RadioStyle;
9
10
class RadioItem 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 RadioStyle
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 RadioStyle();
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
            $parentItem = $cliMenu->getItemByIndex($cliMenu->getSelectedItemIndex());
89
90
            $siblings = $parentItem instanceof SplitItem
91
                ? $parentItem->getItems()
92
                : $cliMenu->getItems();
93
94
            $filtered = array_filter(
95
                $siblings,
96
                function (MenuItemInterface $item) {
97
                    return $item instanceof self;
98
                }
99
            );
100
101
            array_walk(
102
                $filtered,
103
                function (RadioItem $item) {
104
                    $item->setUnchecked();
105
                }
106
            );
107
108
            $this->setChecked();
109
            $cliMenu->redraw();
110
111
            return ($this->selectAction)($cliMenu);
112
        };
113
    }
114
115
    /**
116
     * Can the item be selected
117
     */
118
    public function canSelect() : bool
119
    {
120
        return !$this->disabled;
121
    }
122
123
    /**
124
     * Whether or not we are showing item extra
125
     */
126
    public function showsItemExtra() : bool
127
    {
128
        return $this->showItemExtra;
129
    }
130
131
    /**
132
     * Enable showing item extra
133
     */
134
    public function showItemExtra() : void
135
    {
136
        $this->showItemExtra = true;
137
    }
138
139
    /**
140
     * Disable showing item extra
141
     */
142
    public function hideItemExtra() : void
143
    {
144
        $this->showItemExtra = false;
145
    }
146
147
    /**
148
     * Whether or not the item is checked
149
     */
150
    public function getChecked() : bool
151
    {
152
        return $this->checked;
153
    }
154
155
    /**
156
     * Sets checked state to true
157
     */
158
    public function setChecked() : void
159
    {
160
        $this->checked = true;
161
    }
162
163
    /**
164
     * Sets checked state to false
165
     */
166
    public function setUnchecked() : void
167
    {
168
        $this->checked = false;
169
    }
170
171
    /**
172
     * Toggles checked state
173
     */
174
    public function toggle() : void
175
    {
176
        $this->checked = !$this->checked;
177
    }
178
179
    /**
180
     * @return RadioStyle
181
     */
182
    public function getStyle() : ItemStyle
183
    {
184
        return $this->style;
185
    }
186
187
    public function setStyle(RadioStyle $style) : void
188
    {
189
        $this->style = $style;
190
    }
191
}
192