From 0b63427c56d25c46e1d0866eef071718b05991ff Mon Sep 17 00:00:00 2001 From: Much Yusron Arif Date: Thu, 5 Sep 2024 12:19:50 +0700 Subject: [PATCH 1/2] fix grouped manu --- src/Contracts/GroupedMenu.php | 2 + src/Factory.php | 77 ++++++++++++++-------- src/GroupItem.php | 4 +- src/GroupedMenu.php | 117 +++++++++++++++++++++++++++------- src/MenuItem.php | 6 +- 5 files changed, 154 insertions(+), 52 deletions(-) diff --git a/src/Contracts/GroupedMenu.php b/src/Contracts/GroupedMenu.php index 1ca9149..40b7fcd 100644 --- a/src/Contracts/GroupedMenu.php +++ b/src/Contracts/GroupedMenu.php @@ -10,6 +10,7 @@ interface GroupedMenu * @param string $name * @param string $title * @param array|object $attributes + * @param int $sort * * @return static */ @@ -17,6 +18,7 @@ public function add( string $name, string $title, array|object $attributes = [], + int $sort = 0 ): static; /** diff --git a/src/Factory.php b/src/Factory.php index 2e10b0e..370c363 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -4,7 +4,9 @@ namespace Kfn\Menu; +use Exception; use Illuminate\Support\Fluent; +use Throwable; /** * @implements \Kfn\Menu\Contracts\GroupedMenu @@ -21,10 +23,10 @@ class Factory implements \Kfn\Menu\Contracts\GroupedMenu * @param string|null $name */ public function __construct( - string|null $name = null + string|null $name = null, ) { static::$name = $name ?: 'main'; - if (! static::$factory instanceof Fluent) { + if (!static::$factory instanceof Fluent) { static::$factory = new Fluent(); } } @@ -43,14 +45,18 @@ public function add( string $name, string $title, object|array $attributes = [], - int $sort = 0 + int $sort = 0, ): static { - if (! static::$factory[static::$name] instanceof GroupedMenu) { - static::$factory[static::$name] = new GroupedMenu( - name: $name, - title: $title, - attributes: $attributes - ); + if (!static::$factory[static::$name] instanceof GroupedMenu) { + static::$factory[static::$name] = new GroupedMenu(); + } + if (!static::$factory[static::$name]->has($name)) { + static::$factory[static::$name]->add([ + 'name' => $name, + 'title' => $title, + 'attributes' => $attributes, + 'sort' => $sort, + ]); } return $this; @@ -63,32 +69,49 @@ public function add( * @param bool $resolvedOnly * * @return \Kfn\Menu\GroupedMenu|\Kfn\Menu\GroupItem + * @throws \Throwable */ public function get( string|null $groupName = null, - bool $resolvedOnly = true + bool $resolvedOnly = true, ): GroupedMenu|GroupItem { - $groupedMenu = static::$factory[static::$name]; + try { + $groupedMenu = static::$factory->get(static::$name); + if (! $groupedMenu instanceof GroupedMenu) { + $groupedMenu = new GroupedMenu(); + } - if ($groupName) { - $groupedMenu = $groupedMenu->get($groupName); - } + if (!$groupedMenu instanceof GroupedMenu) { + throw new Exception('menu not yet initialized'); + } - if ($groupedMenu instanceof GroupedMenu && $resolvedOnly) { - $groupedMenu = $groupedMenu->each(function (GroupItem $group) { - $groupItems = $group->items->filter(fn ($it) => $it->resolve()); - $group->items = $groupItems; + if ($groupName) { + $groupedMenu = $groupedMenu->get($groupName); + if (! $groupedMenu instanceof GroupItem) { + $groupedMenu = new GroupItem(); + } + } - return $group; - }); - } + if ($groupedMenu instanceof GroupedMenu && $resolvedOnly && $groupedMenu->isNotEmpty()) { + $groupedMenu = $groupedMenu->each(function (GroupItem $group) { + if ($group->items->isNotEmpty()) { + $groupItems = $group->items->filter(fn(MenuItem $it) => $it->resolve()); + $group->items = $groupItems; + } - // throw_if(app()->hasDebugModeEnabled(), $e); - // app('log')->error('failed on get menu factory\n', [ - // 'message' => $e->getMessage(), - // 'traces' => $e->getTraceAsString(), - // ]); + return $group; + }); + } + + return $groupedMenu; + } catch (Throwable $e) { + throw_if(app()->hasDebugModeEnabled(), $e); + app('log')->error('failed on get menu factory\n', [ + 'message' => $e->getMessage(), + 'traces' => $e->getTraceAsString(), + ]); + } - return $groupedMenu; + return new GroupedMenu(); } } diff --git a/src/GroupItem.php b/src/GroupItem.php index b549156..73bc163 100644 --- a/src/GroupItem.php +++ b/src/GroupItem.php @@ -53,8 +53,8 @@ class GroupItem implements \Kfn\Menu\Contracts\GroupItem * @param int $sort */ public function __construct( - string $name, - string $title, + string $name = 'default', + string $title = 'Default', array|object $attribute = [], int $sort = 0 ) { diff --git a/src/GroupedMenu.php b/src/GroupedMenu.php index 36102f6..42113b8 100644 --- a/src/GroupedMenu.php +++ b/src/GroupedMenu.php @@ -2,38 +2,111 @@ namespace Kfn\Menu; +use Exception; use Illuminate\Support\Collection; +use Illuminate\Support\Fluent; class GroupedMenu extends Collection { /** @var string */ private static string $collectionName; - public function __construct( - string $name = 'default', - string $title = 'Default', - array|object $attributes = [], - int $sort = 0 - ) { - if (empty($name)) { - $name = str($title)->slug()->toString(); + /** + * @param $items + */ + public function __construct($items = []) + { + parent::__construct([]); + } + + /** + * @param $item + * + * @return $this + * @throws \Exception + */ + public function add($item): static + { + $item = $this->_setItem($item); + + return $this->put($item->get('name'), $item->toArray()); + } + + /** + * @param ...$values + * + * @return $this + * @throws \Exception + */ + public function push(...$values): static + { + foreach ($values as $value) { + $this->add($value); } - parent::__construct([ - $name => new GroupItem( - name: $name, - title: $title, - attribute: $attributes, - sort: $sort - ), - ]); + return $this; } - public static function init( - string $name = 'default', - string $title = 'Default', - array $attributes = [] - ): static { - return new static($name, $title, $attributes); + /** + * @param $key + * @param $value + * + * @return void + * @throws \Exception + */ + public function offsetSet($key, $value): void + { + $value = $this->_setItem($value); + + parent::offsetSet($key, new GroupItem( + name: $value->get('name'), + title: $value->get('title'), + attribute: $value->get('attributes'), + sort: $value->get('sort'), + )); } + + /** + * @param mixed $item + * + * @return \Illuminate\Support\Fluent + * @throws \Exception + */ + private function _setItem(mixed $item): Fluent + { + if (! (is_string($item) || is_array($item) || is_object($item))) { + throw new Exception('an item must be a string or an array'); + } + + if (is_string($item)) { + $name = str($item)->slug()->toString(); + $title = $item; + $attributes = []; + $sort = 0; + } else { + if (is_array($item) || is_object($item)) { + $item = new Fluent($item); + } + $name = $item->get('name') + ?: str($item->get('title'))->slug()->toString(); + $title = $item->get('title') ?: $name; + $attributes = $item->get('attributes') ?: []; + $sort = $item->get('sort') ?: 0; + } + + return new Fluent([ + 'name' => $name, + 'title' => $title, + 'attributes' => $attributes, + 'sort' => $sort, + ]); + } + + // public static function init( + // string $name = 'default', + // string $title = 'Default', + // array $attributes = [] + // ): static { + // return new static($name, $title, $attributes); + // } } diff --git a/src/MenuItem.php b/src/MenuItem.php index f32b8d4..93ed618 100644 --- a/src/MenuItem.php +++ b/src/MenuItem.php @@ -59,7 +59,6 @@ public function __construct( $this->attribute = $attribute; $this->items = new MenuCollection(); - $this->resolveHref(); } /** @@ -75,6 +74,8 @@ public function getHref(): string */ public function resolve(): bool { + $this->resolveHref(); + if ($this->resolver instanceof \Closure) { return (bool) $this->resolver->call($this); } @@ -102,6 +103,9 @@ private function resolveHref(): void if ($name->isEmpty()) { throw new Exception('Menu item attribute name is empty'); } + if ($name->is('#')) { + throw new Exception('hashed link'); + } $name = $name->toString(); $this->href = match ($this->type) { From bcafb48146e768f7752967ecb2e27d1586584527 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 5 Sep 2024 05:41:52 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Factory.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index 370c363..94d07da 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -26,7 +26,7 @@ public function __construct( string|null $name = null, ) { static::$name = $name ?: 'main'; - if (!static::$factory instanceof Fluent) { + if (! static::$factory instanceof Fluent) { static::$factory = new Fluent(); } } @@ -47,10 +47,10 @@ public function add( object|array $attributes = [], int $sort = 0, ): static { - if (!static::$factory[static::$name] instanceof GroupedMenu) { + if (! static::$factory[static::$name] instanceof GroupedMenu) { static::$factory[static::$name] = new GroupedMenu(); } - if (!static::$factory[static::$name]->has($name)) { + if (! static::$factory[static::$name]->has($name)) { static::$factory[static::$name]->add([ 'name' => $name, 'title' => $title, @@ -81,7 +81,7 @@ public function get( $groupedMenu = new GroupedMenu(); } - if (!$groupedMenu instanceof GroupedMenu) { + if (! $groupedMenu instanceof GroupedMenu) { throw new Exception('menu not yet initialized'); } @@ -95,7 +95,7 @@ public function get( if ($groupedMenu instanceof GroupedMenu && $resolvedOnly && $groupedMenu->isNotEmpty()) { $groupedMenu = $groupedMenu->each(function (GroupItem $group) { if ($group->items->isNotEmpty()) { - $groupItems = $group->items->filter(fn(MenuItem $it) => $it->resolve()); + $groupItems = $group->items->filter(fn (MenuItem $it) => $it->resolve()); $group->items = $groupItems; }