Skip to content

Commit 0080858

Browse files
committed
chore(Facade): update menu storing method
1 parent 797987c commit 0080858

File tree

1 file changed

+61
-30
lines changed

1 file changed

+61
-30
lines changed

src/Facade/RegisterMenu.php

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,84 @@
99
class RegisterMenu extends Facade
1010
{
1111
protected static array $menus = [];
12-
protected static string|null $currentGroup = null;
12+
protected static array $groups = ['index' => 0, 'group_name' => ''];
1313

14-
public static function groupping(string $label): void
14+
protected static function getFacadeAccessor()
1515
{
16-
static::$currentGroup = $label;
17-
18-
if (!isset(static::$menus[$label])) {
19-
static::$menus[$label] = [
20-
'label' => $label,
21-
'children' => [],
22-
];
23-
}
16+
return 'sukristyan.menu';
2417
}
2518

26-
public static function endGroup(): void
19+
public static function all(): array
2720
{
28-
static::$currentGroup = null;
21+
return static::$menus;
2922
}
3023

31-
public static function add(Route $route, string $label): void
24+
protected static function add(Route $route, string $label, string $groupLabel = '')
3225
{
33-
if (static::$currentGroup) {
34-
static::$menus[static::$currentGroup]['children'][] = [
35-
'route_name' => $route->getName(),
36-
'uri' => $route->uri(),
37-
'label' => $label,
38-
];
26+
if (!empty($groupLabel)) {
27+
self::createAsGroup($route, $label, $groupLabel);
3928
} else {
40-
static::$menus[] = [
41-
'route_name' => $route->getName(),
42-
'uri' => $route->uri(),
43-
'label' => $label,
44-
];
29+
static::$menus[] = array_merge(
30+
['label' => $label],
31+
config('laravel-menu-wrapper.populate_items')($route)
32+
);
4533
}
4634
}
4735

48-
public static function all(): array
36+
private static function createAsGroup(Route $route, string $label, string $groupLabel): array
4937
{
50-
return static::$menus;
38+
if (
39+
!in_array(
40+
$given = config('laravel-menu-wrapper.group_as', 'key'),
41+
$expect = ['item', 'key'],
42+
true
43+
)
44+
) {
45+
throw InvalidGroupException::create($given, $expect);
46+
}
47+
48+
foreach (static::$menus as $id => $menu) {
49+
if (is_array($menu) && ($menu['group_name'] ?? null) === $groupLabel) {
50+
static::$groups = ['id' => $id, 'group_name' => $groupLabel];
51+
return match ($given) {
52+
'key' => self::groupAsKey($route, $label, $groupLabel),
53+
'item' => self::groupAsItem($route, $label, $groupLabel),
54+
};
55+
}
56+
}
57+
58+
$newId = count(static::$menus);
59+
static::$groups = ['id' => $newId, 'group_name' => $groupLabel];
60+
61+
return match ($given) {
62+
'key' => self::groupAsKey($route, $label, $groupLabel),
63+
'item' => self::groupAsItem($route, $label, $groupLabel),
64+
};
5165
}
5266

53-
private static function groupAs()
67+
private static function groupAsKey(Route $route, string $label, string $groupLabel): array
5468
{
55-
if (!in_array($given = config('laravel-menu-wrapper.group_as'), $expect = ['item', 'key'])) {
56-
throw InvalidGroupException::create($given, $expect);
69+
return static::$menus[$groupLabel][] = array_merge(
70+
['label' => $label],
71+
config('laravel-menu-wrapper.populate_items')($route)
72+
);
73+
}
74+
75+
private static function groupAsItem(Route $route, string $label, string $groupLabel): array
76+
{
77+
$id = static::$groups['id'];
78+
79+
if (!isset(static::$menus[$id]['childs'])) {
80+
static::$menus[$id] = [
81+
'group_name' => $groupLabel,
82+
'childs' => []
83+
];
5784
}
5885

59-
return config('laravel-menu-wrapper.group_as');
86+
static::$menus[$id]['childs'][] = array_merge(
87+
['label' => $label],
88+
config('laravel-menu-wrapper.populate_items')($route)
89+
);
90+
return static::$menus[$id];
6091
}
6192
}

0 commit comments

Comments
 (0)