|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Iatstuti\SimpleMenu; |
|
4
|
|
|
|
|
5
|
|
|
use Iatstuti\SimpleMenu\Traits\ObjectOptions; |
|
6
|
|
|
use Iatstuti\Support\Traits\MethodPropertyAccess; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* POPO object to wrap a menu collection. |
|
11
|
|
|
* |
|
12
|
|
|
* @package Iatstuti\SimpleMenu |
|
13
|
|
|
* @copyright 2016 IATSTUTI |
|
14
|
|
|
* @author Michael Dyrynda <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Menu |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
use MethodPropertyAccess, ObjectOptions; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Store the menu items. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Illuminate\Support\Collection |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $items; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Store the menu label. |
|
30
|
|
|
* |
|
31
|
|
|
* @var null|string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $label; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \Iatstuti\SimpleMenu\Presenters\MenuPresenter |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $presenter; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $options; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Menu constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string|null $label |
|
50
|
|
|
* @param array $options |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($label = null, array $options = [ ]) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->label = $label; |
|
55
|
|
|
$this->items = new Collection(); |
|
56
|
|
|
$this->options = array_merge([ 'weight' => 0, ], $options); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return this menu's label. |
|
62
|
|
|
* |
|
63
|
|
|
* @return null|string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function label() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->label; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add a new link to the menu. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $item |
|
75
|
|
|
* @param string $link |
|
76
|
|
|
* @param array $options |
|
77
|
|
|
* |
|
78
|
|
|
* @return $this |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
|
|
public function link($item, $link, array $options = [ ]) |
|
81
|
|
|
{ |
|
82
|
|
|
$item = new MenuItem($item, $link, $options); |
|
83
|
|
|
|
|
84
|
|
|
$this->items()->push($item); |
|
85
|
|
|
|
|
86
|
|
|
$this->sortItems(); |
|
87
|
|
|
|
|
88
|
|
|
return $item; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return this menu's items. |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Illuminate\Support\Collection |
|
96
|
|
|
*/ |
|
97
|
|
|
public function items() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->items; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
private function sortItems() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->items = $this->items->sortBy(function ($item) { |
|
109
|
|
|
return $item->weight(); |
|
110
|
|
|
}); |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Add a new sub menu item to the menu. |
|
118
|
|
|
* |
|
119
|
|
|
* @param \Iatstuti\SimpleMenu\Menu $menu |
|
120
|
|
|
* |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
|
|
public function subMenu(Menu $menu) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->items->push($menu); |
|
126
|
|
|
|
|
127
|
|
|
$this->sortItems(); |
|
128
|
|
|
|
|
129
|
|
|
return $menu; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Return this menu's weight. |
|
135
|
|
|
* |
|
136
|
|
|
* @return int |
|
|
|
|
|
|
137
|
|
|
*/ |
|
138
|
|
|
public function weight() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->options('weight'); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.