|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sukristyan\LaravelMenuWrapper\Facade; |
| 4 | + |
| 5 | +use Illuminate\Routing\Route; |
| 6 | +use Illuminate\Support\Facades\Facade; |
| 7 | +use Sukristyan\LaravelMenuWrapper\Exceptions\InvalidGroupException; |
| 8 | + |
| 9 | +class RegisterMenu extends Facade |
| 10 | +{ |
| 11 | + protected static array $menus = []; |
| 12 | + protected static array $groups = ['index' => 0, 'group_name' => '']; |
| 13 | + |
| 14 | + protected static function getFacadeAccessor() |
| 15 | + { |
| 16 | + return 'sukristyan.menu'; |
| 17 | + } |
| 18 | + |
| 19 | + public static function all(): array |
| 20 | + { |
| 21 | + return static::$menus; |
| 22 | + } |
| 23 | + |
| 24 | + protected static function add(Route $route, string $label, string $groupLabel = '') |
| 25 | + { |
| 26 | + if (!empty($groupLabel)) { |
| 27 | + self::createAsGroup($route, $label, $groupLabel); |
| 28 | + } else { |
| 29 | + static::$menus[] = array_merge( |
| 30 | + ['label' => $label], |
| 31 | + config('laravel-menu-wrapper.populate_items')($route) |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static function createAsGroup(Route $route, string $label, string $groupLabel): array |
| 37 | + { |
| 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 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + private static function groupAsKey(Route $route, string $label, string $groupLabel): array |
| 68 | + { |
| 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 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + static::$menus[$id]['childs'][] = array_merge( |
| 87 | + ['label' => $label], |
| 88 | + config('laravel-menu-wrapper.populate_items')($route) |
| 89 | + ); |
| 90 | + return static::$menus[$id]; |
| 91 | + } |
| 92 | +} |
0 commit comments