FleetCart/Modules/Menu/MegaMenu/Menu.php

103 lines
1.7 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\Menu\MegaMenu;
use Modules\Category\Entities\Category;
class Menu
{
private $menu;
private $subMenus;
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function __construct($menu)
{
$this->menu = $menu;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function isFluid()
{
return $this->menu->is_fluid;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function url()
{
return $this->menu->url();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function hasIcon()
{
2023-12-03 14:07:47 +00:00
return !is_null($this->icon());
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function icon()
{
return $this->menu->icon;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function hasBackgroundImage()
{
2023-12-03 14:07:47 +00:00
return !is_null($this->backgroundImage());
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function backgroundImage()
{
return $this->menu->background_image->path;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function target()
{
return $this->menu->target;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function name()
{
return $this->menu->name;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function hasSubMenus()
{
return $this->subMenus()->isNotEmpty();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
public function subMenus()
{
2023-12-03 14:07:47 +00:00
if (!is_null($this->subMenus)) {
2023-06-11 12:14:03 +00:00
return $this->subMenus;
}
return $this->subMenus = $this->getSubMenus()->map(function ($subMenu) {
return new SubMenu($subMenu);
});
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getSubMenus()
{
if ($this->menu->isCategoryType()) {
return $this->getCategorySubMenus();
}
return $this->menu->items ?? collect();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getCategorySubMenus()
{
return Category::tree()
->where('id', $this->menu->category_id)
->first()
->items ?? collect();
}
}