Completed
Pull Request — master (#127)
by Aydin
01:53
created

BuilderUtils::addLineBreak()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace PhpSchool\CliMenu;
4
5
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
6
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
7
use PhpSchool\CliMenu\MenuItem\SelectableItem;
8
use PhpSchool\CliMenu\MenuItem\StaticItem;
9
use RuntimeException;
10
11
/**
12
 * @author Aydin Hassan <[email protected]>
13
 */
14
trait BuilderUtils
15
{
16
    /**
17
     * @var null|Builder
18
     */
19
    private $parent;
20
21
    /**
22
     * @var self[]
23
     */
24
    private $subMenuBuilders = [];
25
26
    /**
27
     * @var CliMenu[]
28
     */
29
    private $subMenus = [];
30
31
    /**
32
     * @var array
33
     */
34
    private $menuItems = [];
35
36
    public function addItem(
37
        string $text,
38
        callable $itemCallable,
39
        bool $showItemExtra = false,
40
        bool $disabled = false
41
    ) : self {
42
        $this->menuItems[] = new SelectableItem($text, $itemCallable, $showItemExtra, $disabled);
43
44
        return $this;
45
    }
46
47
    public function addStaticItem(string $text) : self
48
    {
49
        $this->menuItems[] = new StaticItem($text);
50
51
        return $this;
52
    }
53
54
    public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
55
    {
56
        $this->menuItems[] = new LineBreakItem($breakChar, $lines);
57
58
        return $this;
59
    }
60
    
61
    /**
62
     * Add a submenu with a name. The name will be displayed as the item text
63
     * in the parent menu.
64
     */
65
    public function addSubMenu(string $name, CliMenuBuilder $subMenuBuilder = null) : Builder
66
    {
67
        $this->menuItems[]  = $id = 'submenu-placeholder-' . $name;
68
69
        if (null === $subMenuBuilder) {
70
            $this->subMenuBuilders[$id] = new CliMenuBuilder($this);
71
            return $this->subMenuBuilders[$id];
72
        }
73
74
        $this->subMenuBuilders[$id] = $subMenuBuilder;
75
        return $this;
76
    }
77
78
    private function buildSubMenus(array $items) : array
79
    {
80
        return array_map(function ($item) {
81
            if (!is_string($item) || 0 !== strpos($item, 'submenu-placeholder-')) {
82
                return $item;
83
            }
84
85
            $menuBuilder           = $this->subMenuBuilders[$item];
86
            $this->subMenus[$item] = $menuBuilder->build();
87
88
            return new MenuMenuItem(
89
                substr($item, \strlen('submenu-placeholder-')),
90
                $this->subMenus[$item],
91
                $menuBuilder->isMenuDisabled()
92
            );
93
        }, $items);
94
    }
95
96
    /**
97
     * Return to parent builder
98
     *
99
     * @throws RuntimeException
100
     */
101
    public function end() : ?Builder
102
    {
103
        if (null === $this->parent) {
104
            throw new RuntimeException('No parent builder to return to');
105
        }
106
107
        return $this->parent;
108
    }
109
}
110