Completed
Push — master ( a9e6a3...c72b58 )
by Aydin
01:55
created

SplitItemBuilder::addLineBreak()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
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 addCheckboxItem(
63
        string $text,
64
        callable $itemCallable,
65
        bool $showItemExtra = false,
66
        bool $disabled = false
67
    ) : self {
68
        $this->splitItem->addItem(new CheckboxItem($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($builder);
107
108
        $menu = $builder->build();
109
        $menu->setParent($this->menu);
110
111
        $this->splitItem->addItem(new MenuMenuItem(
112
            $text,
113
            $menu,
114
            $builder->isMenuDisabled()
115
        ));
116
        
117
        return $this;
118
    }
119
120
    public function setGutter(int $gutter) : self
121
    {
122
        $this->splitItem->setGutter($gutter);
123
        
124
        return $this;
125
    }
126
127
    public function enableAutoShortcuts(string $regex = null) : self
128
    {
129
        $this->autoShortcuts = true;
130
131
        if (null !== $regex) {
132
            $this->autoShortcutsRegex = $regex;
133
        }
134
135
        return $this;
136
    }
137
    
138
    public function build() : SplitItem
139
    {
140
        return $this->splitItem;
141
    }
142
}
143