1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Menus\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Route; |
8
|
|
|
use Collective\Html\HtmlFacade as HTML; |
9
|
|
|
use Illuminate\Support\Facades\Request; |
10
|
|
|
|
11
|
|
|
class MenuItem |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The properties array. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
public $properties = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The childs collection. |
22
|
|
|
* |
23
|
|
|
* @var \Illuminate\Support\Collection |
24
|
|
|
*/ |
25
|
|
|
protected $childs; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The hide callback. |
29
|
|
|
* |
30
|
|
|
* @var callable |
31
|
|
|
*/ |
32
|
|
|
protected $hideWhen; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The active callback. |
36
|
|
|
* |
37
|
|
|
* @var callable |
38
|
|
|
*/ |
39
|
|
|
protected $activeWhen; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param array $properties |
45
|
|
|
*/ |
46
|
|
|
public function __construct($properties = []) |
47
|
|
|
{ |
48
|
|
|
$this->fill($properties); |
49
|
|
|
$this->childs = collect(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get property. |
54
|
|
|
* |
55
|
|
|
* @param string $key |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function __get($key) |
60
|
|
|
{ |
61
|
|
|
return data_get($this->properties, $key); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Fill the properties. |
66
|
|
|
* |
67
|
|
|
* @param array $properties |
68
|
|
|
* |
69
|
|
|
* @return static |
70
|
|
|
*/ |
71
|
|
|
public function fill($properties) |
72
|
|
|
{ |
73
|
|
|
$this->properties = array_merge($this->properties, $properties); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create new menu with dropdown. |
80
|
|
|
* |
81
|
|
|
* @param callable $callback |
82
|
|
|
* @param string $title |
83
|
|
|
* @param int $order |
|
|
|
|
84
|
|
|
* @param string $icon |
|
|
|
|
85
|
|
|
* @param array $attributes |
86
|
|
|
* |
87
|
|
|
* @return static |
88
|
|
|
*/ |
89
|
|
|
public function dropdown(callable $callback, string $title, int $order = null, string $icon = null, array $attributes = []) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
call_user_func($callback, $item = $this->add(compact('title', 'order', 'icon', 'attributes'))); |
92
|
|
|
|
93
|
|
|
return $item; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Register new menu item using registered route. |
98
|
|
|
* |
99
|
|
|
* @param string $route |
|
|
|
|
100
|
|
|
* @param string $title |
101
|
|
|
* @param int $order |
|
|
|
|
102
|
|
|
* @param string $icon |
|
|
|
|
103
|
|
|
* @param array $attributes |
104
|
|
|
* |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
|
|
public function route(array $route, string $title, int $order = null, string $icon = null, array $attributes = []) |
108
|
|
|
{ |
109
|
|
|
return $this->add(compact('route', 'title', 'order', 'icon', 'attributes')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Register new menu item using url. |
114
|
|
|
* |
115
|
|
|
* @param string $url |
116
|
|
|
* @param string $title |
117
|
|
|
* @param int $order |
|
|
|
|
118
|
|
|
* @param string $icon |
|
|
|
|
119
|
|
|
* @param array $attributes |
120
|
|
|
* |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
|
|
public function url(string $url, string $title, int $order = null, string $icon = null, array $attributes = []) |
124
|
|
|
{ |
125
|
|
|
return $this->add(compact('url', 'title', 'order', 'icon', 'attributes')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add new header item. |
130
|
|
|
* |
131
|
|
|
* @param string $title |
132
|
|
|
* @param int $order |
|
|
|
|
133
|
|
|
* @param string $icon |
|
|
|
|
134
|
|
|
* @param array $attributes |
135
|
|
|
* |
136
|
|
|
* @return static |
137
|
|
|
*/ |
138
|
|
|
public function header(string $title, int $order = null, string $icon = null, array $attributes = []) |
139
|
|
|
{ |
140
|
|
|
$type = 'header'; |
141
|
|
|
|
142
|
|
|
return $this->add(compact('type', 'url', 'title', 'order', 'icon', 'attributes')); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Add new divider item. |
147
|
|
|
* |
148
|
|
|
* @param int $order |
|
|
|
|
149
|
|
|
* @param array $attributes |
150
|
|
|
* |
151
|
|
|
* @return static |
152
|
|
|
*/ |
153
|
|
|
public function divider(int $order = null, array $attributes = []) |
154
|
|
|
{ |
155
|
|
|
return $this->add(['type' => 'divider', 'order' => $order, 'attributes' => $attributes]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get childs. |
160
|
|
|
* |
161
|
|
|
* @return \Illuminate\Support\Collection |
162
|
|
|
*/ |
163
|
|
|
public function getChilds() |
164
|
|
|
{ |
165
|
|
|
return $this->childs->sortBy('properties.order'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get url. |
170
|
|
|
* |
171
|
|
|
* @return string |
|
|
|
|
172
|
|
|
*/ |
173
|
|
|
public function getUrl() |
174
|
|
|
{ |
175
|
|
|
return $this->route ? route($this->route[0], $this->route[1] ?? []) : ($this->url ? url($this->url) : ''); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get HTML attribute data. |
180
|
|
|
* |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
|
|
public function getAttributes() |
184
|
|
|
{ |
185
|
|
|
return HTML::attributes($this->attributes); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Check if the current item is divider. |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function isDivider(): bool |
194
|
|
|
{ |
195
|
|
|
return $this->type === 'divider'; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check if the current item is header. |
200
|
|
|
* |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
public function isHeader(): bool |
204
|
|
|
{ |
205
|
|
|
return $this->type === 'header'; |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Check is the current item has sub menu . |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
public function hasChilds() |
214
|
|
|
{ |
215
|
|
|
return $this->childs->isNotEmpty(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Check the active state for current menu. |
220
|
|
|
* |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function hasActiveOnChild() |
224
|
|
|
{ |
225
|
|
|
return $this->hasChilds() ? $this->hasActiveStateFromChilds() : false; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set hide callback for current menu item. |
230
|
|
|
* |
231
|
|
|
* @param callable $callback |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function hideWhen(callable $callback) |
236
|
|
|
{ |
237
|
|
|
$this->hideWhen = $callback; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Set authorization callback for current menu item. |
244
|
|
|
* |
245
|
|
|
* @param string $ability |
246
|
|
|
* @param mixed $params |
247
|
|
|
* |
248
|
|
|
* @return $this |
249
|
|
|
*/ |
250
|
|
|
public function ifCan(string $ability, $params = null) |
251
|
|
|
{ |
252
|
|
|
$this->hideWhen = function () use ($ability, $params) { |
253
|
|
|
return ! auth()->user()->can($ability, $params); |
|
|
|
|
254
|
|
|
}; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Set authentication callback for current menu item. |
261
|
|
|
* |
262
|
|
|
* @return $this |
263
|
|
|
*/ |
264
|
|
|
public function ifUser() |
265
|
|
|
{ |
266
|
|
|
$this->hideWhen = function () { |
267
|
|
|
return ! auth()->user(); |
|
|
|
|
268
|
|
|
}; |
269
|
|
|
|
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Set authentication callback for current menu item. |
275
|
|
|
* |
276
|
|
|
* @return $this |
277
|
|
|
*/ |
278
|
|
|
public function ifGuest() |
279
|
|
|
{ |
280
|
|
|
$this->hideWhen = function () { |
281
|
|
|
return auth()->user(); |
|
|
|
|
282
|
|
|
}; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Check if the menu item is hidden. |
289
|
|
|
* |
290
|
|
|
* @return bool |
291
|
|
|
*/ |
292
|
|
|
public function isHidden() |
293
|
|
|
{ |
294
|
|
|
return $this->hideWhen ? (bool) call_user_func($this->hideWhen) : false; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Get active state for current item. |
299
|
|
|
* |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
|
|
public function isActive() |
303
|
|
|
{ |
304
|
|
|
if (is_callable($activeWhen = $this->activeWhen)) { |
305
|
|
|
return call_user_func($activeWhen); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if ($this->route) { |
|
|
|
|
309
|
|
|
return $this->hasActiveStateFromRoute(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $this->hasActiveStateFromUrl(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Set active callback. |
317
|
|
|
* |
318
|
|
|
* @param callable $route |
|
|
|
|
319
|
|
|
* |
320
|
|
|
* @return $this |
321
|
|
|
*/ |
322
|
|
|
public function activateWhen(callable $callback) |
323
|
|
|
{ |
324
|
|
|
$this->activeWhen = $callback; |
325
|
|
|
|
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Set active callback on the given route. |
331
|
|
|
* |
332
|
|
|
* @param string $route |
333
|
|
|
* |
334
|
|
|
* @return $this |
335
|
|
|
*/ |
336
|
|
|
public function activateOnRoute(string $route) |
337
|
|
|
{ |
338
|
|
|
$this->activeWhen = function () use ($route) { |
339
|
|
|
return str_contains(Route::currentRouteName(), $route); |
340
|
|
|
}; |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Add new child item. |
347
|
|
|
* |
348
|
|
|
* @param array $properties |
349
|
|
|
* |
350
|
|
|
* @return static |
351
|
|
|
*/ |
352
|
|
|
protected function add(array $properties = []) |
353
|
|
|
{ |
354
|
|
|
$properties['attributes']['id'] = $properties['attributes']['id'] ?? md5(json_encode($properties)); |
355
|
|
|
$this->childs->push($item = new static($properties)); |
356
|
|
|
|
357
|
|
|
return $item; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Get active status using route. |
362
|
|
|
* |
363
|
|
|
* @return bool |
364
|
|
|
*/ |
365
|
|
|
protected function hasActiveStateFromRoute(): bool |
366
|
|
|
{ |
367
|
|
|
return Route::is($this->route); |
|
|
|
|
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Get active status using request url. |
372
|
|
|
* |
373
|
|
|
* @return bool |
374
|
|
|
*/ |
375
|
|
|
protected function hasActiveStateFromUrl(): bool |
376
|
|
|
{ |
377
|
|
|
return Request::is($this->url); |
|
|
|
|
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Check if the item has active state from childs. |
382
|
|
|
* |
383
|
|
|
* @return bool |
384
|
|
|
*/ |
385
|
|
|
protected function hasActiveStateFromChilds(): bool |
386
|
|
|
{ |
387
|
|
|
return $this->getChilds()->contains(function (MenuItem $child) { |
388
|
|
|
return ($child->hasChilds() && $child->hasActiveStateFromChilds()) |
389
|
|
|
|| ($child->route && $child->hasActiveStateFromRoute()) |
|
|
|
|
390
|
|
|
|| $child->isActive() || $child->hasActiveStateFromUrl(); |
391
|
|
|
}) ?? false; |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.