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