Skip to content

Commit

Permalink
fix: allow closure for title
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetan-hexadog committed Sep 6, 2023
1 parent 265afee commit d631d53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hexadog\MenusManager;

use Closure;
use Hexadog\MenusManager\Traits\HasItems;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -76,7 +77,13 @@ public function __get($key)
return $this->properties;
}

return Arr::get($this->properties, $key);
$value = Arr::get($this->properties, $key);

if ($value instanceof Closure) {
$value = $value();
}

return $value;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Traits/HasItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hexadog\MenusManager\Traits;

use Closure;
use Hexadog\MenusManager\Item;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -68,9 +69,9 @@ public function findBy(string $key, string $value): ?Item
*
* @return mixed
*/
public function findByTitleOrAdd(string $title, array $attributes = []): ?Item
public function findByTitleOrAdd(string|Closure $title, array $attributes = []): ?Item
{
if (!($item = $this->findBy('title', $title))) {
if (!($item = $this->findBy('title', $title instanceof Closure ? $title() : $title))) {
$item = $this->add(compact('title', 'attributes'));
}

Expand All @@ -80,7 +81,7 @@ public function findByTitleOrAdd(string $title, array $attributes = []): ?Item
/**
* Add new header menu item.
*/
public function header(string $title, array $attributes = []): Item
public function header(string|Closure $title, array $attributes = []): Item
{
return $this->add(compact('title', 'attributes'))->asHeader();
}
Expand All @@ -102,15 +103,15 @@ public function items()
*
* @param mixed $route
*/
public function route($route, string $title, array $attributes = []): Item
public function route($route, string|Closure $title, array $attributes = []): Item
{
return $this->add(compact('route', 'title', 'attributes'));
}

/**
* Register new menu item using url.
*/
public function url(string $url, string $title, array $attributes = []): Item
public function url(string $url, string|Closure $title, array $attributes = []): Item
{
return $this->add(compact('url', 'title', 'attributes'));
}
Expand Down

0 comments on commit d631d53

Please sign in to comment.