Passed
Pull Request — master (#205)
by Aydin
02:02
created

SplitItemBuilder::setCheckboxStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace PhpSchool\CliMenu\Builder;
4
5
use PhpSchool\CliMenu\CliMenu;
6
use PhpSchool\CliMenu\MenuItem\CheckboxItem;
7
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
8
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
9
use PhpSchool\CliMenu\MenuItem\RadioItem;
10
use PhpSchool\CliMenu\MenuItem\SelectableItem;
11
use PhpSchool\CliMenu\MenuItem\SplitItem;
12
use PhpSchool\CliMenu\MenuItem\StaticItem;
13
use PhpSchool\CliMenu\Style\CheckboxStyle;
14
use PhpSchool\CliMenu\Style\RadioStyle;
15
16
/**
17
 * @author Aydin Hassan <[email protected]>
18
 */
19
class SplitItemBuilder
20
{
21
    /**
22
     * @var CliMenu
23
     */
24
    private $menu;
25
26
    /**
27
     * @var SplitItem
28
     */
29
    private $splitItem;
30
31
    /**
32
     * Whether or not to auto create keyboard shortcuts for items
33
     * when they contain square brackets. Eg: [M]y item
34
     *
35
     * @var bool
36
     */
37
    private $autoShortcuts = false;
38
39
    /**
40
     * Regex to auto match for shortcuts defaults to looking
41
     * for a single character encased in square brackets
42
     *
43
     * @var string
44
     */
45
    private $autoShortcutsRegex = '/\[(.)\]/';
46
47
    public function __construct(CliMenu $menu)
48
    {
49
        $this->menu = $menu;
50
        $this->splitItem = new SplitItem();
51
    }
52
53
    public function addItem(
54
        string $text,
55
        callable $itemCallable,
56
        bool $showItemExtra = false,
57
        bool $disabled = false
58
    ) : self {
59
        $this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled));
60
61
        return $this;
62
    }
63
64
    public function addCheckboxItem(
65
        string $text,
66
        callable $itemCallable,
67
        bool $showItemExtra = false,
68
        bool $disabled = false
69
    ) : self {
70
        $item = (new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled))
71
            ->setStyle($this->menu->getCheckboxStyle());
72
73
        $this->splitItem->addItem($item);
74
75
        return $this;
76
    }
77
78
    public function addRadioItem(
79
        string $text,
80
        callable $itemCallable,
81
        bool $showItemExtra = false,
82
        bool $disabled = false
83
    ) : self {
84
        $item = (new RadioItem($text, $itemCallable, $showItemExtra, $disabled))
85
            ->setStyle($this->menu->getRadioStyle());
86
87
        $this->splitItem->addItem($item);
88
89
        return $this;
90
    }
91
92
    public function addStaticItem(string $text) : self
93
    {
94
        $this->splitItem->addItem(new StaticItem($text));
95
96
        return $this;
97
    }
98
99
    public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
100
    {
101
        $this->splitItem->addItem(new LineBreakItem($breakChar, $lines));
102
103
        return $this;
104
    }
105
106
    public function addSubMenu(string $text, \Closure $callback) : self
107
    {
108
        $builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal());
109
110
        if ($this->autoShortcuts) {
111
            $builder->enableAutoShortcuts($this->autoShortcutsRegex);
112
        }
113
114
        $callback($builder);
115
116
        $menu = $builder->build();
117
        $menu->setParent($this->menu);
118
119
        if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) {
120
            $menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle());
121
        }
122
123
        if (!$menu->getRadioStyle()->hasChangedFromDefaults()) {
124
            $menu->setRadioStyle(clone $this->menu->getRadioStyle());
125
        }
126
127
        $this->splitItem->addItem(new MenuMenuItem(
128
            $text,
129
            $menu,
130
            $builder->isMenuDisabled()
131
        ));
132
        
133
        return $this;
134
    }
135
136
    public function setGutter(int $gutter) : self
137
    {
138
        $this->splitItem->setGutter($gutter);
139
        
140
        return $this;
141
    }
142
143
    public function enableAutoShortcuts(string $regex = null) : self
144
    {
145
        $this->autoShortcuts = true;
146
147
        if (null !== $regex) {
148
            $this->autoShortcutsRegex = $regex;
149
        }
150
151
        return $this;
152
    }
153
    
154
    public function build() : SplitItem
155
    {
156
        return $this->splitItem;
157
    }
158
159
    public function getCheckboxStyle() : CheckboxStyle
160
    {
161
        return $this->menu->getCheckboxStyle();
162
    }
163
164
    public function setCheckboxStyle(CheckboxStyle $style) : self
165
    {
166
        $this->menu->setCheckboxStyle($style);
167
168
        return $this;
169
    }
170
171
    public function modifyCheckboxStyle(callable $itemCallable) : self
172
    {
173
        $itemCallable($this->menu->getCheckboxStyle());
174
175
        return $this;
176
    }
177
178
    public function getRadioStyle() : RadioStyle
179
    {
180
        return $this->menu->getRadioStyle();
181
    }
182
183
    public function setRadioStyle(RadioStyle $style) : self
184
    {
185
        $this->menu->setRadioStyle($style);
186
187
        return $this;
188
    }
189
190
    public function modifyRadioStyle(callable $itemCallable) : self
191
    {
192
        $itemCallable($this->menu->getRadioStyle());
193
194
        return $this;
195
    }
196
}
197