Completed
Push — master ( d80c74...3c4db9 )
by Aydin
14s queued 11s
created

CheckableItem::setText()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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 CheckableItem 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 $checked = 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
     * @param MenuStyle $style
76
     * @param bool $selected Currently unused in this class
77
     * @return array
78
     */
79
    public function getRows(MenuStyle $style, bool $selected = false) : array
80
    {
81
        $marker = sprintf("%s", $this->checked ? $style->getCheckedMarker() : $style->getUncheckedMarker());
82
83
        $length = $style->getDisplaysExtra()
84
            ? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2)
85
            : $style->getContentWidth();
86
87
        $rows = explode(
88
            "\n",
89
            StringUtil::wordwrap(
90
                sprintf('%s%s', $marker, $this->text),
91
                $length,
92
                sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
93
            )
94
        );
95
96
        return array_map(function ($row, $key) use ($style, $length) {
97
            $text = $this->disabled ? $style->getDisabledItemText($row) : $row;
98
99
            if ($key === 0) {
100
                return $this->showItemExtra
101
                    ? sprintf('%s%s  %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra())
102
                    : $text;
103
            }
104
105
            return $text;
106
        }, $rows, array_keys($rows));
107
    }
108
109
    /**
110
     * Can the item be selected
111
     */
112
    public function canSelect() : bool
113
    {
114
        return !$this->disabled;
115
    }
116
117
    public function showsItemExtra() : bool
118
    {
119
        return $this->showItemExtra;
120
    }
121
122
    /**
123
     * Enable showing item extra
124
     */
125
    public function showItemExtra() : void
126
    {
127
        $this->showItemExtra = true;
128
    }
129
130
    /**
131
     * Disable showing item extra
132
     */
133
    public function hideItemExtra() : void
134
    {
135
        $this->showItemExtra = false;
136
    }
137
138
    /**
139
     * Toggles checked state
140
     */
141
    public function toggle()
142
    {
143
        $this->checked = !$this->checked;
144
    }
145
146
    /**
147
     * Sets checked state to true
148
     */
149
    public function setChecked()
150
    {
151
        $this->checked = true;
152
    }
153
154
    /**
155
     * Sets checked state to false
156
     */
157
    public function setUnchecked()
158
    {
159
        $this->checked = false;
160
    }
161
162
    public function getChecked(): bool
163
    {
164
        return $this->checked;
165
    }
166
}
167