Skip to content

Commit

Permalink
add activeRoute param
Browse files Browse the repository at this point in the history
  • Loading branch information
yusronarif committed Nov 4, 2022
1 parent 553a992 commit 8f99def
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"name": "koffinate/laravel-menu",
"description": "Laravel Menu Collector",
"keywords": ["php", "laravel", "library", "much", "yusron", "arif", "yusronarif"],
"keywords": [
"php", "laravel", "library",
"koffinate", "koffin", "coffee",
"much", "yusron", "arif", "yusronarif"
],
"type": "library",
"license": "MIT",
"authors": [
Expand Down
16 changes: 12 additions & 4 deletions helpers.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php

use Koffin\Menu\Enum\MenuType;
use Koffin\Menu\Menu;

if (!function_exists('menus')) {
/**
* @param null|string $menu
* Menu instance
*
* @param ?string $menu
* @param ?string $group
*
* @return Menu
*/
function menus(?string $menu = null, ?string $group = null): Menu
Expand All @@ -17,10 +22,13 @@ function menus(?string $menu = null, ?string $group = null): Menu
/**
* Menu Type Enum
*
* @return MenuType
* @return ?MenuType
*/
function menuType(): string
function menuType(?string $type): ?MenuType
{
return "\\Koffin\\Menu\\Enum\\MenuType";
if ($type) {
MenuType::tryFrom($type);
}
return new MenuType;
}
}
2 changes: 1 addition & 1 deletion src/Enum/MenuType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

enum MenuType: string
{
case Route = 'route';
case ROUTE = 'route';
case URL = 'url';
}
43 changes: 35 additions & 8 deletions src/Menu.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Koffin\Menu;

use Closure;
Expand All @@ -15,10 +17,10 @@ class Menu

public function __construct(?string $menu = null, ?string $group = null)
{
if (!empty($menu)) {
if ($menu) {
static::$menu = $menu;
}
if (!empty($group)) {
if ($group) {
static::$group = $group;
}
}
Expand All @@ -34,21 +36,46 @@ public function get(): Collection
return collect();
}

public static function route(string $name, string $title, array $attribute = [], array $param = [], Closure|bool $resolver = true): static
public static function route(string $name, string $title, array $attribute = [], array $param = [], ?string $activeRoute = null, Closure|bool $resolver = true): static
{
return static::add(type: MenuType::Route, name: $name, title: $title, attribute: $attribute, param: $param, resolver: $resolver);
return static::add(
type: MenuType::ROUTE,
name: $name,
title: $title,
attribute: $attribute,
param: $param,
activeRoute: $activeRoute,
resolver: $resolver,
);
}

public static function url(string $name, string $title, array $attribute = [], array $param = [], Closure|bool $resolver = true): static
public static function url(string $name, string $title, array $attribute = [], array $param = [], ?string $activeRoute = null, Closure|bool $resolver = true): static
{
return static::add(type: MenuType::URL, name: $name, title: $title, attribute: $attribute, param: $param, resolver: $resolver);
return static::add(
type: MenuType::URL,
name: $name,
title: $title,
attribute: $attribute,
param: $param,
activeRoute: $activeRoute,
resolver: $resolver,
);
}

public static function add(MenuType $type, string $name, string $title, array $attribute = [], array $param = [], Closure|bool $resolver = true): static
public static function add(MenuType $type, string $name, string $title, array $attribute = [], array $param = [], ?string $activeRoute = null, Closure|bool $resolver = true): static
{
$factory = static::getFactory();
$factory->add(
new MenuItem(type: $type, name: $name, title: $title, attribute: $attribute, param: $param, group: static::$group, resolver: $resolver)
new MenuItem(
type: $type,
name: $name,
title: $title,
attribute: $attribute,
param: $param,
activeRoute:$activeRoute,
group: static::$group,
resolver: $resolver,
)
);
return new static();
}
Expand Down
5 changes: 3 additions & 2 deletions src/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct(
public array $attribute = [],
public string $name,
public array $param = [],
public ?string $activeRoute = null,
public string $group = 'Default',
public \Closure|bool $resolver = true,
)
Expand All @@ -29,13 +30,13 @@ public function resolve(): bool
public function isActive(): bool
{
if ($this->type == MenuType::Route) {
return $this->activeRoute($this->name, $this->param);
return $this->isActiveRoute($this->activeRoute ?? $this->name, $this->param);
}

return false;
}

private function activeRoute(string $route = '', array $params = []): bool
private function isActiveRoute(string $route = '', array $params = []): bool
{
if (empty($route = trim($route))) {
return false;
Expand Down

0 comments on commit 8f99def

Please sign in to comment.