Completed
Push — master ( 867de8...1b7820 )
by Aydin
20s queued 12s
created

SplitItemBuilder::setGutter()   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\CheckableItem;
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
14
/**
15
 * @author Aydin Hassan <[email protected]>
16
 */
17
class SplitItemBuilder
18
{
19
    /**
20
     * @var CliMenu
21
     */
22
    private $menu;
23
24
    /**
25
     * @var SplitItem
26
     */
27
    private $splitItem;
28
29
    /**
30
     * Whether or not to auto create keyboard shortcuts for items
31
     * when they contain square brackets. Eg: [M]y item
32
     *
33
     * @var bool
34
     */
35
    private $autoShortcuts = false;
36
37
    /**
38
     * Regex to auto match for shortcuts defaults to looking
39
     * for a single character encased in square brackets
40
     *
41
     * @var string
42
     */
43
    private $autoShortcutsRegex = '/\[(.)\]/';
44
45
    public function __construct(CliMenu $menu)
46
    {
47
        $this->menu = $menu;
48
        $this->splitItem = new SplitItem();
49
    }
50
51
    public function addItem(
52
        string $text,
53
        callable $itemCallable,
54
        bool $showItemExtra = false,
55
        bool $disabled = false
56
    ) : self {
57
        $this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled));
58
59
        return $this;
60
    }
61
62
    public function addCheckableItem(
63
        string $text,
64
        callable $itemCallable,
65
        bool $showItemExtra = false,
66
        bool $disabled = false
67
    ) : self {
68
        $this->splitItem->addItem(new CheckableItem($text, $itemCallable, $showItemExtra, $disabled));
69
70
        return $this;
71
    }
72
73
    public function addRadioItem(
74
        string $text,
75
        callable $itemCallable,
76
        bool $showItemExtra = false,
77
        bool $disabled = false
78
    ) : self {
79
        $this->splitItem->addItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled));
80
81
        return $this;
82
    }
83
84
    public function addStaticItem(string $text) : self
85
    {
86
        $this->splitItem->addItem(new StaticItem($text));
87
88
        return $this;
89
    }
90
91
    public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
92
    {
93
        $this->splitItem->addItem(new LineBreakItem($breakChar, $lines));
94
95
        return $this;
96
    }
97
98
    public function addSubMenu(string $text, \Closure $callback) : self
99
    {
100
        $builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal());
101
102
        if ($this->autoShortcuts) {
103
            $builder->enableAutoShortcuts($this->autoShortcutsRegex);
104
        }
105
106
        $callback = $callback->bindTo($builder);
107
        $callback($builder);
108
109
        $menu = $builder->build();
110
        $menu->setParent($this->menu);
111
112
        $this->splitItem->addItem(new MenuMenuItem(
113
            $text,
114
            $menu,
115
            $builder->isMenuDisabled()
116
        ));
117
        
118
        return $this;
119
    }
120
121
    public function setGutter(int $gutter) : self
122
    {
123
        $this->splitItem->setGutter($gutter);
124
        
125
        return $this;
126
    }
127
128
    public function enableAutoShortcuts(string $regex = null) : self
129
    {
130
        $this->autoShortcuts = true;
131
132
        if (null !== $regex) {
133
            $this->autoShortcutsRegex = $regex;
134
        }
135
136
        return $this;
137
    }
138
    
139
    public function build() : SplitItem
140
    {
141
        return $this->splitItem;
142
    }
143
}
144