Completed
Push — master ( 499201...4a0bb5 )
by Aydin
26s queued 12s
created

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