45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Service;
|
||
|
|
||
|
use Incoviba\Repository;
|
||
|
|
||
|
class Menu
|
||
|
{
|
||
|
public function __construct(protected Repository\Menu $menuRepository, protected Permission $permissionService, protected object $urls) {}
|
||
|
|
||
|
public function build(int $user_id): string
|
||
|
{
|
||
|
$menus = $this->getValid($user_id);
|
||
|
$output = [];
|
||
|
foreach ($menus as $menu) {
|
||
|
$output []= $this->buildItem($menu);
|
||
|
}
|
||
|
return implode(PHP_EOL, $output);
|
||
|
}
|
||
|
protected function buildItem(mixed $item): string
|
||
|
{
|
||
|
if (isset($item->submenus)) {
|
||
|
return $this->buildDropdown($item);
|
||
|
}
|
||
|
return "<a class=\"item\" href=\"{{$this->urls->base}}/{{$item->url}}\">{{$item->title}}</a>";
|
||
|
}
|
||
|
protected function buildDropdown(mixed $item): string
|
||
|
{
|
||
|
$output []= '<div class="ui simple dropdown item">';
|
||
|
$output []= $item->title;
|
||
|
$output []= '<i class="dropdown icon"></i>';
|
||
|
$output []= '<div class="menu">';
|
||
|
foreach ($item->submenus as $menu) {
|
||
|
$output []= $this->buildItem($menu);
|
||
|
}
|
||
|
$output []= '</div>';
|
||
|
$output []= '</div>';
|
||
|
return implode(PHP_EOL, $output);
|
||
|
}
|
||
|
|
||
|
public function getValid(int $user_id): array
|
||
|
{
|
||
|
return $this->menuRepository->fetchByUser($user_id);
|
||
|
}
|
||
|
}
|