Skip to content

Commit 72d2b82

Browse files
committed
Separate adding submenu and adding submenu via existing builder
1 parent 1baaefa commit 72d2b82

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/Builder/BuilderUtils.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
5959
}
6060

6161
/**
62-
* Add a submenu with a name. The name will be displayed as the item text
62+
* Add a submenu with a unique ID and the text. The text will be displayed as the item text
6363
* in the parent menu.
6464
*/
65-
public function addSubMenu(string $id, string $text, CliMenuBuilder $subMenuBuilder = null) : Builder
65+
public function addSubMenu(string $id, string $text) : CliMenuBuilder
6666
{
6767
if (isset($this->subMenuBuilders[$id])) {
6868
throw new \InvalidArgumentException(
@@ -76,11 +76,28 @@ public function addSubMenu(string $id, string $text, CliMenuBuilder $subMenuBuil
7676
'id' => $id
7777
];
7878

79-
if (null === $subMenuBuilder) {
80-
$this->subMenuBuilders[$id] = new CliMenuBuilder($this);
81-
return $this->subMenuBuilders[$id];
79+
$this->subMenuBuilders[$id] = new CliMenuBuilder($this);
80+
return $this->subMenuBuilders[$id];
81+
}
82+
83+
/**
84+
* Add a submenu from an existing builder. Required a unique ID and the text. The text will be displayed as the
85+
* item text in the parent menu.
86+
*/
87+
public function addSubMenuFromExistingBuilder(string $id, string $text, CliMenuBuilder $subMenuBuilder) : self
88+
{
89+
if (isset($this->subMenuBuilders[$id])) {
90+
throw new \InvalidArgumentException(
91+
sprintf('SubMenu with id: "%s" already exists. $id must be unique', $id)
92+
);
8293
}
8394

95+
$this->menuItems[] = [
96+
'type' => 'submenu-placeholder',
97+
'text' => $text,
98+
'id' => $id
99+
];
100+
84101
$this->subMenuBuilders[$id] = $subMenuBuilder;
85102
return $this;
86103
}

0 commit comments

Comments
 (0)