Completed
Push — master ( 2edd9d...d97347 )
by Aydin
21s queued 11s
created

MenuMenuItem::propagateStyles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\CliMenu;
6
use PhpSchool\CliMenu\MenuStyle;
7
use PhpSchool\CliMenu\Util\StringUtil;
8
use PhpSchool\CliMenu\Style\ItemStyle;
9
use PhpSchool\CliMenu\Style\SelectableStyle;
10
use function PhpSchool\CliMenu\Util\mapWithKeys;
11
12
/**
13
 * @author Michael Woodward <[email protected]>
14
 */
15
class MenuMenuItem implements MenuItemInterface, PropagatesStyles
16
{
17
    /**
18
     * @var string
19
     */
20
    private $text;
21
22
    /**
23
     * @var CliMenu
24
     */
25
    private $subMenu;
26
27
    /**
28
     * @var bool
29
     */
30
    private $showItemExtra = false;
31
32
    /**
33
     * @var bool
34
     */
35
    private $disabled;
36
37
    /**
38
     * @var SelectableStyle
39
     */
40
    private $style;
41
42
    public function __construct(
43
        string $text,
44
        CliMenu $subMenu,
45
        bool $disabled = false
46
    ) {
47
        $this->text = $text;
48
        $this->subMenu = $subMenu;
49
        $this->disabled = $disabled;
50
51
        $this->style = new SelectableStyle();
52
    }
53
54
    /**
55
     * The output text for the item
56
     */
57
    public function getRows(MenuStyle $style, bool $selected = false) : array
58
    {
59
        $marker = sprintf("%s", $this->style->getMarker($this, $selected));
60
61
        $length = $this->style->getDisplaysExtra()
62
            ? $style->getContentWidth() - (mb_strlen($this->style->getItemExtra()) + 2)
63
            : $style->getContentWidth();
64
65
        $rows = explode(
66
            "\n",
67
            StringUtil::wordwrap(
68
                sprintf('%s%s', $marker, $this->text),
69
                $length,
70
                sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
71
            )
72
        );
73
74
        return mapWithKeys($rows, function (int $key, string $row) use ($style, $length) {
75
            $text = $this->disabled ? $style->getDisabledItemText($row) : $row;
76
77
            if ($key === 0) {
78
                return $this->showItemExtra
79
                    ? sprintf(
80
                        '%s%s  %s',
81
                        $text,
82
                        str_repeat(' ', $length - mb_strlen($row)),
83
                        $this->style->getItemExtra()
84
                    )
85
                    : $text;
86
            }
87
88
            return $text;
89
        });
90
    }
91
92
    /**
93
     * Return the raw string of text
94
     */
95
    public function getText() : string
96
    {
97
        return $this->text;
98
    }
99
100
    /**
101
     * Set the raw string of text
102
     */
103
    public function setText(string $text) : void
104
    {
105
        $this->text = $text;
106
    }
107
108
    /**
109
     * Execute the items callable if required
110
     */
111
    public function getSelectAction() : ?callable
112
    {
113
        return function (CliMenu $menu) {
114
            $this->showSubMenu($menu);
115
        };
116
    }
117
118
    /**
119
     * Returns the sub menu
120
     */
121
    public function getSubMenu() : CliMenu
122
    {
123
        return $this->subMenu;
124
    }
125
126
    /**
127
     * Display the sub menu
128
     */
129
    public function showSubMenu(CliMenu $parentMenu) : void
130
    {
131
        $parentMenu->closeThis();
132
        $this->subMenu->open();
133
    }
134
135
    /**
136
     * Can the item be selected
137
     */
138
    public function canSelect() : bool
139
    {
140
        return !$this->disabled;
141
    }
142
143
    /**
144
     * Enable showing item extra
145
     */
146
    public function showItemExtra() : void
147
    {
148
        $this->showItemExtra = true;
149
    }
150
151
    /**
152
     * Whether or not we are showing item extra
153
     */
154
    public function showsItemExtra() : bool
155
    {
156
        return $this->showItemExtra;
157
    }
158
159
    /**
160
     * Disable showing item extra
161
     */
162
    public function hideItemExtra() : void
163
    {
164
        $this->showItemExtra = false;
165
    }
166
167
    /**
168
     * @return SelectableStyle
169
     */
170
    public function getStyle() : ItemStyle
171
    {
172
        return $this->style;
173
    }
174
175
    public function setStyle(SelectableStyle $style) : void
176
    {
177
        $this->style = $style;
178
    }
179
180
    /**
181
     * @inheritDoc
182
     */
183
    public function propagateStyles(CliMenu $parent): void
184
    {
185
        $this->getSubMenu()->importStyles($parent);
186
        $this->getSubMenu()->propagateStyles();
187
    }
188
}
189