-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathMenuBuilder.php
646 lines (555 loc) · 13 KB
/
MenuBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?php
namespace Pingpong\Menus;
use Countable;
use Illuminate\Config\Repository;
use Illuminate\View\Factory as ViewFactory;
class MenuBuilder implements Countable
{
/**
* Menu name.
*
* @var string
*/
protected $menu;
/**
* Array menu items.
*
* @var array
*/
protected $items = array();
/**
* Default presenter class.
*
* @var string
*/
protected $presenter = 'Pingpong\Menus\Presenters\Bootstrap\NavbarPresenter';
/**
* Style name for each presenter.
*
* @var array
*/
protected $styles = array();
/**
* Prefix URL.
*
* @var string|null
*/
protected $prefixUrl = null;
/**
* The name of view presenter.
*
* @var null
*/
protected $view = null;
/**
* The laravel view factory instance.
*
* @var \Illumiate\View\Factory
*/
protected $views;
/**
* Determine whether the ordering feature is enabled or not.
*
* @var boolean
*/
protected $ordering = false;
/**
* Resolved item binding map.
*
* @var array
*/
protected $bindings = array();
/**
* Constructor.
*
* @param string $menu
*/
public function __construct($menu, Repository $config)
{
$this->menu = $menu;
$this->config = $config;
}
/**
* Get menu name.
*
* @return string
*/
public function getName()
{
return $this->menu;
}
/**
* Find menu item by given its title.
*
* @param string $title
* @param callable|null $callback
* @return mixed
*/
public function whereTitle($title, callable $callback = null)
{
$item = $this->findBy('title', $title);
if (is_callable($callback)) {
return call_user_func($callback, $item);
}
return $item;
}
/**
* Find menu item by given key and value.
*
* @param string $key
* @param string $value
* @return \Pingpong\Menus\MenuItem
*/
public function findBy($key, $value)
{
return collect($this->items)->filter(function ($item) use ($key, $value) {
return $item->{$key} == $value;
})->first();
}
/**
* Set view factory instance.
*
* @param ViewFactory $views
*
* @return $this
*/
public function setViewFactory(ViewFactory $views)
{
$this->views = $views;
return $this;
}
/**
* Set view.
*
* @param string $view
*
* @return $this
*/
public function setView($view)
{
$this->view = $view;
return $this;
}
/**
* Set Prefix URL.
*
* @param string $prefixUrl
*
* @return $this
*/
public function setPrefixUrl($prefixUrl)
{
$this->prefixUrl = $prefixUrl;
return $this;
}
/**
* Set styles.
*
* @param array $styles
*/
public function setStyles(array $styles)
{
$this->styles = $styles;
}
/**
* Set new presenter class.
*
* @param string $presenter
*/
public function setPresenter($presenter)
{
$this->presenter = $presenter;
}
/**
* Get presenter instance.
*
* @return \Pingpong\Menus\Presenters\PresenterInterface
*/
public function getPresenter()
{
return new $this->presenter();
}
/**
* Set new presenter class by given style name.
*
* @param string $name
*
* @return self
*/
public function style($name)
{
if ($this->hasStyle($name)) {
$this->setPresenter($this->getStyle($name));
}
return $this;
}
/**
* Determine if the given name in the presenter style.
*
* @param $name
*
* @return bool
*/
public function hasStyle($name)
{
return array_key_exists($name, $this->getStyles());
}
/**
* Get style aliases.
*
* @return mixed
*/
public function getStyles()
{
return $this->styles ?: $this->config->get('menus.styles');
}
/**
* Get the presenter class name by given alias name.
*
* @param $name
*
* @return mixed
*/
public function getStyle($name)
{
$style = $this->getStyles();
return $style[$name];
}
/**
* Set new presenter class from given alias name.
*
* @param $name
*/
public function setPresenterFromStyle($name)
{
$this->setPresenter($this->getStyle($name));
}
/**
* Set the resolved item bindings
*
* @param array $arr
*/
public function setBindings(array $bindings)
{
$this->bindings = $bindings;
return $this;
}
/**
* Resolves a key from the bindings array.
*
* @param string|array $key
* @return mixed
*/
public function resolve($key)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$key[$k] = $this->resolve($v);
}
} elseif (is_string($key)) {
$matches = array();
preg_match_all('/{[\s]*?([^\s]+)[\s]*?}/i', $key, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
if (array_key_exists($match[1], $this->bindings)) {
$key = preg_replace('/' . $match[0] . '/', $this->bindings[$match[1]], $key, 1);
}
}
}
return $key;
}
/**
* Resolves an array of menu items properties.
*
* @param array &$items
* @return void
*/
protected function resolveItems(array &$items)
{
$resolver = function ($property) {
return $this->resolve($property) ?: $property;
};
for ($i = 0; $i < count($items); $i++) {
$items[$i]->fill(array_map($resolver, $items[$i]->getProperties()));
}
}
/**
* Add new child menu.
*
* @param array $attributes
*
* @return \Pingpong\Menus\MenuItem
*/
public function add(array $attributes = array())
{
$item = MenuItem::make($attributes);
$this->items[] = $item;
return $item;
}
/**
* Create new menu with dropdown.
*
* @param $title
* @param callable $callback
* @param array $attributes
*
* @return $this
*/
public function dropdown($title, \Closure $callback, $order = null, array $attributes = array())
{
$properties = compact('title', 'order', 'attributes');
if (func_num_args() == 3) {
$arguments = func_get_args();
$title = array_get($arguments, 0);
$attributes = array_get($arguments, 2);
$properties = compact('title', 'attributes');
}
$item = MenuItem::make($properties);
call_user_func($callback, $item);
$this->items[] = $item;
return $item;
}
/**
* Register new menu item using registered route.
*
* @param $route
* @param $title
* @param array $parameters
* @param array $attributes
*
* @return static
*/
public function route($route, $title, $parameters = array(), $order = null, $attributes = array())
{
if (func_num_args() == 4) {
$arguments = func_get_args();
return $this->add([
'route' => [array_get($arguments, 0), array_get($arguments, 2)],
'title' => array_get($arguments, 1),
'attributes' => array_get($arguments, 3)
]);
}
$route = array($route, $parameters);
$item = MenuItem::make(
compact('route', 'title', 'parameters', 'attributes', 'order')
);
$this->items[] = $item;
return $item;
}
/**
* Format URL.
*
* @param string $url
*
* @return string
*/
protected function formatUrl($url)
{
$uri = !is_null($this->prefixUrl) ? $this->prefixUrl.$url : $url;
return $uri == '/' ? '/' : ltrim(rtrim($uri, '/'), '/');
}
/**
* Register new menu item using url.
*
* @param $url
* @param $title
* @param array $attributes
*
* @return static
*/
public function url($url, $title, $order = 0, $attributes = array())
{
if (func_num_args() == 3) {
$arguments = func_get_args();
return $this->add([
'url' => $this->formatUrl(array_get($arguments, 0)),
'title' => array_get($arguments, 1),
'attributes' => array_get($arguments, 2)
]);
}
$url = $this->formatUrl($url);
$item = MenuItem::make(compact('url', 'title', 'order', 'attributes'));
$this->items[] = $item;
return $item;
}
/**
* Add new divider item.
*
* @param int $order
* @return \Pingpong\Menus\MenuItem
*/
public function addDivider($order = null)
{
$this->items[] = new MenuItem(array('name' => 'divider', 'order' => $order));
return $this;
}
/**
* Add new header item.
*
* @return \Pingpong\Menus\MenuItem
*/
public function addHeader($title, $order = null)
{
$this->items[] = new MenuItem(array(
'name' => 'header',
'title' => $title,
'order' => $order
));
return $this;
}
/**
* Alias for "addHeader" method.
*
* @param string $title
*
* @return $this
*/
public function header($title)
{
return $this->addHeader($title);
}
/**
* Alias for "addDivider" method.
*
* @return $this
*/
public function divider()
{
return $this->addDivider();
}
/**
* Get items count.
*
* @return int
*/
public function count()
{
return count($this->items);
}
/**
* Empty the current menu items.
*/
public function destroy()
{
$this->items = array();
return $this;
}
/**
* Render the menu to HTML tag.
*
* @param string $presenter
*
* @return string
*/
public function render($presenter = null)
{
$this->resolveItems($this->items);
if (!is_null($this->view)) {
return $this->renderView($presenter);
}
if ($this->hasStyle($presenter)) {
$this->setPresenterFromStyle($presenter);
}
if (!is_null($presenter) && !$this->hasStyle($presenter)) {
$this->setPresenter($presenter);
}
return $this->renderMenu();
}
/**
* Render menu via view presenter.
*
* @return \Illuminate\View\View
*/
public function renderView($presenter = null)
{
return $this->views->make($presenter ?: $this->view, [
'items' => $this->getOrderedItems(),
]);
}
/**
* Get original items.
*
* @return array
*/
public function getItems()
{
return $this->items;
}
/**
* Get menu items as laravel collection instance.
*
* @return \Illuminate\Support\Collection
*/
public function toCollection()
{
return collect($this->items);
}
/**
* Get menu items as array.
*
* @return array
*/
public function toArray()
{
return $this->toCollection()->toArray();
}
/**
* Enable menu ordering.
*
* @return self
*/
public function enableOrdering()
{
$this->ordering = true;
return $this;
}
/**
* Disable menu ordering.
*
* @return self
*/
public function disableOrdering()
{
$this->ordering = false;
return $this;
}
/**
* Get menu items and order it by 'order' key.
*
* @return array
*/
public function getOrderedItems()
{
if (config('menus.ordering') || $this->ordering) {
return $this->toCollection()->sortBy(function ($item) {
return $item->order;
})->all();
}
return $this->items;
}
/**
* Render the menu.
*
* @return string
*/
protected function renderMenu()
{
$presenter = $this->getPresenter();
$menu = $presenter->getOpenTagWrapper();
foreach ($this->getOrderedItems() as $item) {
if ($item->hidden()) {
continue;
}
if ($item->hasSubMenu()) {
$menu .= $presenter->getMenuWithDropDownWrapper($item);
} elseif ($item->isHeader()) {
$menu .= $presenter->getHeaderWrapper($item);
} elseif ($item->isDivider()) {
$menu .= $presenter->getDividerWrapper();
} else {
$menu .= $presenter->getMenuWithoutDropdownWrapper($item);
}
}
$menu .= $presenter->getCloseTagWrapper();
return $menu;
}
}