Skip to content

Commit

Permalink
Submenu refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandedeyne committed Aug 1, 2016
1 parent 7e340c9 commit 77c7067
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,8 @@ public function submenu($header, $menu = null)
{
list($header, $menu) = $this->parseSubmenuArgs(func_get_args());

if (is_callable($menu)) {
$transformer = $menu;
$menu = $this->blueprint();
$transformer($menu);
}

if ($header instanceof Item) {
$header = $header->render();
}
$menu = $this->createSubmenuMenu($menu);
$header = $this->createSubmenuHeader($header);

return $this->add($menu->prependIf($header, $header));
}
Expand All @@ -212,6 +205,36 @@ protected function parseSubmenuArgs($args): array
return [$args[0], $args[1]];
}

/**
* @param \Spatie\Menu\Menu|callable $menu
*
* @return \Spatie\Menu\Menu
*/
protected function createSubmenuMenu($menu): Menu
{
if (is_callable($menu)) {
$transformer = $menu;
$menu = $this->blueprint();
$transformer($menu);
}

return $menu;
}

/**
* @param \Spatie\Menu\Item|string $header
*
* @return string
*/
protected function createSubmenuHeader($header): string
{
if ($header instanceof Item) {
$header = $header->render();
}

return $header;
}

/**
* Iterate over all the items and apply a callback. If you typehint the
* item parameter in the callable, it wil only be applied to items of that
Expand Down

0 comments on commit 77c7067

Please sign in to comment.