|
2 | 2 |
|
3 | 3 | namespace Sukristyan\LaravelMenuWrapper\Facade; |
4 | 4 |
|
| 5 | +use Illuminate\Routing\Route; |
5 | 6 | use Illuminate\Support\Facades\Facade; |
| 7 | +use Sukristyan\LaravelMenuWrapper\Exceptions\InvalidGroupException; |
6 | 8 |
|
7 | 9 | class RegisterMenu extends Facade |
8 | 10 | { |
9 | | - protected static $menus = []; |
10 | | - protected static $currentGroup = null; |
| 11 | + protected static array $menus = []; |
| 12 | + protected static string|null $currentGroup = null; |
11 | 13 |
|
12 | | - public static function groupping($label) |
| 14 | + public static function groupping(string $label): void |
13 | 15 | { |
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 | + } |
16 | 24 | } |
17 | 25 |
|
18 | | - public static function endGroupping() |
| 26 | + public static function endGroup(): void |
19 | 27 | { |
20 | | - self::$currentGroup = null; |
| 28 | + static::$currentGroup = null; |
21 | 29 | } |
22 | 30 |
|
23 | | - public static function add($uri, $label) |
| 31 | + public static function add(Route $route, string $label): void |
24 | 32 | { |
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 | + ]; |
27 | 45 | } |
28 | 46 | } |
29 | 47 |
|
30 | | - public static function all() |
| 48 | + public static function all(): array |
31 | 49 | { |
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'); |
33 | 60 | } |
34 | 61 | } |
0 commit comments