Completed
Pull Request — master (#194)
by
unknown
01:57
created

RadioItem   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 113
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A canSelect() 0 3 1
A showsItemExtra() 0 3 1
A hideItemExtra() 0 3 1
A setText() 0 3 1
A showItemExtra() 0 3 1
A getText() 0 3 1
A getSelectAction() 0 28 2
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\CliMenu;
6
7
class RadioItem implements MenuItemInterface, ToggableItemInterface
8
{
9
    use ToggableTrait;
10
11
    /**
12
     * @var callable
13
     */
14
    private $selectAction;
15
16
    /**
17
     * @var string
18
     */
19
    private $text = '';
20
21
    /**
22
     * @var bool
23
     */
24
    private $showItemExtra = false;
25
26
    /**
27
     * @var bool
28
     */
29
    private $disabled = false;
30
31
    public function __construct(
32
        string $text,
33
        callable $selectAction,
34
        bool $showItemExtra = false,
35
        bool $disabled = false
36
    ) {
37
        $this->text          = $text;
38
        $this->selectAction  = $selectAction;
39
        $this->showItemExtra = $showItemExtra;
40
        $this->disabled      = $disabled;
41
    }
42
43
    /**
44
     * Execute the items callable if required
45
     */
46
    public function getSelectAction() : ?callable
47
    {
48
        return function (CliMenu $cliMenu) {
49
            $parentItem = $cliMenu->getItemByIndex($cliMenu->getSelectedItemIndex());
50
51
            $siblings = $parentItem instanceof SplitItem
52
                ? $parentItem->getItems()
53
                : $cliMenu->getItems();
54
55
56
            $filtered = array_filter(
57
                $siblings,
58
                function (MenuItemInterface $item) {
59
                    return $item instanceof self;
60
                }
61
            );
62
63
            array_walk(
64
                $filtered,
65
                function (RadioItem $checkableItem) {
66
                    $checkableItem->setUnchecked();
67
                }
68
            );
69
70
            $this->setChecked();
71
            $cliMenu->redraw();
72
73
            return ($this->selectAction)($cliMenu);
74
        };
75
    }
76
77
    /**
78
     * Return the raw string of text
79
     */
80
    public function getText() : string
81
    {
82
        return $this->text;
83
    }
84
85
    /**
86
     * Set the raw string of text
87
     */
88
    public function setText(string $text) : void
89
    {
90
        $this->text = $text;
91
    }
92
93
    /**
94
     * Can the item be selected
95
     */
96
    public function canSelect() : bool
97
    {
98
        return !$this->disabled;
99
    }
100
101
    public function showsItemExtra() : bool
102
    {
103
        return $this->showItemExtra;
104
    }
105
106
    /**
107
     * Enable showing item extra
108
     */
109
    public function showItemExtra() : void
110
    {
111
        $this->showItemExtra = true;
112
    }
113
114
    /**
115
     * Disable showing item extra
116
     */
117
    public function hideItemExtra() : void
118
    {
119
        $this->showItemExtra = false;
120
    }
121
}
122