Skip to content

Commit fa965ac

Browse files
committed
integrate exception & config detection
1 parent 548b752 commit fa965ac

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/Facade/RegisterMenu.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,60 @@
22

33
namespace Sukristyan\LaravelMenuWrapper\Facade;
44

5+
use Illuminate\Routing\Route;
56
use Illuminate\Support\Facades\Facade;
7+
use Sukristyan\LaravelMenuWrapper\Exceptions\InvalidGroupException;
68

79
class RegisterMenu extends Facade
810
{
9-
protected static $menus = [];
10-
protected static $currentGroup = null;
11+
protected static array $menus = [];
12+
protected static string|null $currentGroup = null;
1113

12-
public static function groupping($label)
14+
public static function groupping(string $label): void
1315
{
14-
self::$currentGroup = $label;
15-
self::$menus[$label] = [];
16+
static::$currentGroup = $label;
17+
18+
if (!isset(static::$menus[$label])) {
19+
static::$menus[$label] = [
20+
'label' => $label,
21+
'children' => [],
22+
];
23+
}
1624
}
1725

18-
public static function endGroupping()
26+
public static function endGroup(): void
1927
{
20-
self::$currentGroup = null;
28+
static::$currentGroup = null;
2129
}
2230

23-
public static function add($uri, $label)
31+
public static function add(Route $route, string $label): void
2432
{
25-
if (self::$currentGroup) {
26-
self::$menus[self::$currentGroup][$uri] = $label;
33+
if (static::$currentGroup) {
34+
static::$menus[static::$currentGroup]['children'][] = [
35+
'route_name' => $route->getName(),
36+
'uri' => $route->uri(),
37+
'label' => $label,
38+
];
39+
} else {
40+
static::$menus[] = [
41+
'route_name' => $route->getName(),
42+
'uri' => $route->uri(),
43+
'label' => $label,
44+
];
2745
}
2846
}
2947

30-
public static function all()
48+
public static function all(): array
3149
{
32-
return self::$menus;
50+
return static::$menus;
51+
}
52+
53+
private static function groupAs()
54+
{
55+
if (!in_array($given = config('laravel-menu-wrapper.group_as'), $expect = ['item', 'key'])) {
56+
throw InvalidGroupException::create($given, $expect);
57+
}
58+
59+
return config('laravel-menu-wrapper.group_as');
3360
}
3461
}

0 commit comments

Comments
 (0)