Skip to content

Commit d631d53

Browse files
fix: allow closure for title
1 parent 265afee commit d631d53

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Item.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hexadog\MenusManager;
44

5+
use Closure;
56
use Hexadog\MenusManager\Traits\HasItems;
67
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Support\Arr;
@@ -76,7 +77,13 @@ public function __get($key)
7677
return $this->properties;
7778
}
7879

79-
return Arr::get($this->properties, $key);
80+
$value = Arr::get($this->properties, $key);
81+
82+
if ($value instanceof Closure) {
83+
$value = $value();
84+
}
85+
86+
return $value;
8087
}
8188

8289
/**

src/Traits/HasItems.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hexadog\MenusManager\Traits;
44

5+
use Closure;
56
use Hexadog\MenusManager\Item;
67
use Illuminate\Support\Collection;
78

@@ -68,9 +69,9 @@ public function findBy(string $key, string $value): ?Item
6869
*
6970
* @return mixed
7071
*/
71-
public function findByTitleOrAdd(string $title, array $attributes = []): ?Item
72+
public function findByTitleOrAdd(string|Closure $title, array $attributes = []): ?Item
7273
{
73-
if (!($item = $this->findBy('title', $title))) {
74+
if (!($item = $this->findBy('title', $title instanceof Closure ? $title() : $title))) {
7475
$item = $this->add(compact('title', 'attributes'));
7576
}
7677

@@ -80,7 +81,7 @@ public function findByTitleOrAdd(string $title, array $attributes = []): ?Item
8081
/**
8182
* Add new header menu item.
8283
*/
83-
public function header(string $title, array $attributes = []): Item
84+
public function header(string|Closure $title, array $attributes = []): Item
8485
{
8586
return $this->add(compact('title', 'attributes'))->asHeader();
8687
}
@@ -102,15 +103,15 @@ public function items()
102103
*
103104
* @param mixed $route
104105
*/
105-
public function route($route, string $title, array $attributes = []): Item
106+
public function route($route, string|Closure $title, array $attributes = []): Item
106107
{
107108
return $this->add(compact('route', 'title', 'attributes'));
108109
}
109110

110111
/**
111112
* Register new menu item using url.
112113
*/
113-
public function url(string $url, string $title, array $attributes = []): Item
114+
public function url(string $url, string|Closure $title, array $attributes = []): Item
114115
{
115116
return $this->add(compact('url', 'title', 'attributes'));
116117
}

0 commit comments

Comments
 (0)