Passed
Pull Request — master (#186)
by
unknown
03:03
created

ToggleableItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\MenuItem;
6
use PhpSchool\CliMenu\MenuStyle;
7
use PhpSchool\CliMenu\Util\StringUtil;
8
9
class ToggleableItem implements MenuItem\MenuItemInterface
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
    /**
32
     * @var bool
33
     */
34
    private $toggled = false;
35
36
    public function __construct(
37
        string $text,
38
        callable $selectAction,
39
        bool $showItemExtra = false,
40
        bool $disabled = false
41
    ) {
42
        $this->text          = $text;
43
        $this->selectAction  = $selectAction;
44
        $this->showItemExtra = $showItemExtra;
45
        $this->disabled      = $disabled;
46
    }
47
48
    /**
49
     * Execute the items callable if required
50
     */
51
    public function getSelectAction() : ?callable
52
    {
53
        return $this->selectAction;
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
     * The output text for the item
74
     */
75
    public function getRows(MenuStyle $style, bool $toggled = false) : array
76
    {
77
        $marker = sprintf("%s", $style->getMarkerToggled($this->toggled));
78
79
        $length = $style->getDisplaysExtra()
80
            ? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2)
81
            : $style->getContentWidth();
82
83
        $rows = explode(
84
            "\n",
85
            StringUtil::wordwrap(
86
                sprintf('%s %s', $marker, $this->text),
87
                $length,
88
                sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
89
            )
90
        );
91
92
        return array_map(function ($row, $key) use ($style, $length) {
93
            $text = $this->disabled ? $style->getDisabledItemText($row) : $row;
94
95
            if ($key === 0) {
96
                return $this->showItemExtra
97
                    ? sprintf('%s%s  %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra())
98
                    : $text;
99
            }
100
101
            return $text;
102
        }, $rows, array_keys($rows));
103
    }
104
105
    /**
106
     * Can the item be selected
107
     */
108
    public function canSelect() : bool
109
    {
110
        return !$this->disabled;
111
    }
112
113
    public function showsItemExtra() : bool
114
    {
115
        return $this->showItemExtra;
116
    }
117
118
    /**
119
     * Enable showing item extra
120
     */
121
    public function showItemExtra() : void
122
    {
123
        $this->showItemExtra = true;
124
    }
125
126
    /**
127
     * Disable showing item extra
128
     */
129
    public function hideItemExtra() : void
130
    {
131
        $this->showItemExtra = false;
132
    }
133
134
    public function setToggled(bool $toggled)
135
    {
136
        $this->toggled = $toggled;
137
    }
138
139
    public function getToggled(): bool
140
    {
141
        return $this->toggled;
142
    }
143
}
144