Completed
Push — master ( 867de8...1b7820 )
by Aydin
20s queued 12s
created

CheckableItem::toggle()   A

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
7
class CheckableItem 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
            $this->toggle();
50
            $cliMenu->redraw();
51
52
            return ($this->selectAction)($cliMenu);
53
        };
54
    }
55
56
    /**
57
     * Return the raw string of text
58
     */
59
    public function getText() : string
60
    {
61
        return $this->text;
62
    }
63
64
    /**
65
     * Set the raw string of text
66
     */
67
    public function setText(string $text) : void
68
    {
69
        $this->text = $text;
70
    }
71
72
    /**
73
     * Can the item be selected
74
     */
75
    public function canSelect() : bool
76
    {
77
        return !$this->disabled;
78
    }
79
80
    public function showsItemExtra() : bool
81
    {
82
        return $this->showItemExtra;
83
    }
84
85
    /**
86
     * Enable showing item extra
87
     */
88
    public function showItemExtra() : void
89
    {
90
        $this->showItemExtra = true;
91
    }
92
93
    /**
94
     * Disable showing item extra
95
     */
96
    public function hideItemExtra() : void
97
    {
98
        $this->showItemExtra = false;
99
    }
100
}
101