Skip to content

Commit

Permalink
Merge pull request #8 from koffinate/master
Browse files Browse the repository at this point in the history
fix grouped menu
  • Loading branch information
yusronarif authored Sep 5, 2024
2 parents 329658b + 0fd6dd7 commit be905be
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 50 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/GroupedMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ interface GroupedMenu
* @param string $name
* @param string $title
* @param array|object $attributes
* @param int $sort
*
* @return static
*/
public function add(
string $name,
string $title,
array|object $attributes = [],
int $sort = 0
): static;

/**
Expand Down
73 changes: 48 additions & 25 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Kfn\Menu;

use Exception;
use Illuminate\Support\Fluent;
use Throwable;

/**
* @implements \Kfn\Menu\Contracts\GroupedMenu
Expand All @@ -21,7 +23,7 @@ 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) {
Expand All @@ -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
);
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;
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/GroupItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down
117 changes: 95 additions & 22 deletions src/GroupedMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// }
}
6 changes: 5 additions & 1 deletion src/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function __construct(

$this->attribute = $attribute;
$this->items = new MenuCollection();
$this->resolveHref();
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit be905be

Please sign in to comment.