Completed
Pull Request — master (#194)
by
unknown
03:16 queued 01:09
created

RadioItem::setText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
            $filtered = array_filter(
56
                $siblings,
57
                function (MenuItemInterface $item) {
58
                    return $item instanceof self;
59
                }
60
            );
61
62
            array_walk(
63
                $filtered,
64
                function (RadioItem $checkableItem) {
65
                    $checkableItem->setUnchecked();
66
                }
67
            );
68
69
            $this->setChecked();
70
            $cliMenu->redraw();
71
72
            return ($this->selectAction)($cliMenu);
73
        };
74
    }
75
76
    /**
77
     * Return the raw string of text
78
     */
79
    public function getText() : string
80
    {
81
        return $this->text;
82
    }
83
84
    /**
85
     * Set the raw string of text
86
     */
87
    public function setText(string $text) : void
88
    {
89
        $this->text = $text;
90
    }
91
92
    /**
93
     * Can the item be selected
94
     */
95
    public function canSelect() : bool
96
    {
97
        return !$this->disabled;
98
    }
99
100
    public function showsItemExtra() : bool
101
    {
102
        return $this->showItemExtra;
103
    }
104
105
    /**
106
     * Enable showing item extra
107
     */
108
    public function showItemExtra() : void
109
    {
110
        $this->showItemExtra = true;
111
    }
112
113
    /**
114
     * Disable showing item extra
115
     */
116
    public function hideItemExtra() : void
117
    {
118
        $this->showItemExtra = false;
119
    }
120
}
121