1
|
|
|
<?php namespace Arcanedev\Menus\Entities; |
2
|
|
|
|
3
|
|
|
use Closure; |
4
|
|
|
use IteratorAggregate; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Menu |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\Menus\Entities |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Menu implements IteratorAggregate |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* The menu name. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The menu item collection. |
27
|
|
|
* |
28
|
|
|
* @var MenuItemCollection |
29
|
|
|
*/ |
30
|
|
|
protected $items; |
31
|
|
|
|
32
|
|
|
/* ------------------------------------------------------------------------------------------------ |
33
|
|
|
| Constructor |
34
|
|
|
| ------------------------------------------------------------------------------------------------ |
35
|
|
|
*/ |
36
|
|
|
/** |
37
|
|
|
* Make a Menu instance. |
38
|
|
|
* |
39
|
|
|
* @param string $name |
40
|
|
|
*/ |
41
|
35 |
|
private function __construct($name) |
42
|
|
|
{ |
43
|
35 |
|
$this->name = $name; |
44
|
35 |
|
$this->items = new MenuItemCollection; |
45
|
35 |
|
} |
46
|
|
|
|
47
|
|
|
/* ------------------------------------------------------------------------------------------------ |
48
|
|
|
| Getters & Setters |
49
|
|
|
| ------------------------------------------------------------------------------------------------ |
50
|
|
|
*/ |
51
|
|
|
/** |
52
|
|
|
* Get the menu items iterator. |
53
|
|
|
* |
54
|
|
|
* @return \Arcanedev\Menus\Entities\MenuItemCollection |
55
|
|
|
*/ |
56
|
5 |
|
public function getIterator() |
57
|
|
|
{ |
58
|
5 |
|
return $this->items; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/* ------------------------------------------------------------------------------------------------ |
62
|
|
|
| Main Functions |
63
|
|
|
| ------------------------------------------------------------------------------------------------ |
64
|
|
|
*/ |
65
|
|
|
/** |
66
|
|
|
* Make a menu. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* @param \Closure $callback |
70
|
|
|
* |
71
|
|
|
* @return Menu |
72
|
|
|
*/ |
73
|
35 |
|
public static function make($name, Closure $callback = null) |
74
|
|
|
{ |
75
|
35 |
|
$menu = new self($name); |
76
|
35 |
|
if ( ! is_null($callback)) { |
77
|
20 |
|
call_user_func($callback, $menu); |
78
|
20 |
|
} |
79
|
|
|
|
80
|
35 |
|
return $menu; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add an url item to the menu. |
85
|
|
|
* |
86
|
|
|
* @param string $url |
87
|
|
|
* @param string $content |
88
|
|
|
* @param array $attributes |
89
|
|
|
*/ |
90
|
25 |
|
public function url($url, $content, array $attributes = []) |
91
|
|
|
{ |
92
|
25 |
|
$this->makeItem('url', compact('url', 'content', 'attributes')); |
93
|
25 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add a route item to the menu. |
97
|
|
|
* |
98
|
|
|
* @param string $route |
99
|
|
|
* @param string $content |
100
|
|
|
* @param array $parameters |
101
|
|
|
* @param array $attributes |
102
|
|
|
*/ |
103
|
25 |
|
public function route($route, $content, array $parameters = [], array $attributes = []) |
104
|
|
|
{ |
105
|
25 |
|
$route = [$route, $parameters]; |
106
|
|
|
|
107
|
25 |
|
$this->makeItem('route', compact('route', 'content', 'attributes')); |
108
|
25 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Add a action item to the menu. |
112
|
|
|
* |
113
|
|
|
* @param string $action |
114
|
|
|
* @param string $content |
115
|
|
|
* @param array $parameters |
116
|
|
|
* @param array $attributes |
117
|
|
|
*/ |
118
|
25 |
|
public function action($action, $content, array $parameters = [], array $attributes = []) |
119
|
|
|
{ |
120
|
25 |
|
$action = [$action, $parameters]; |
121
|
|
|
|
122
|
25 |
|
$this->makeItem('action', compact('action', 'content', 'attributes')); |
123
|
25 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Add a dropdown item to the menu. |
127
|
|
|
* |
128
|
|
|
* @param string $content |
129
|
|
|
* @param Closure $callback |
130
|
|
|
* @param array $attributes |
131
|
|
|
*/ |
132
|
15 |
|
public function dropdown($content, Closure $callback, array $attributes = []) |
133
|
|
|
{ |
134
|
15 |
|
$this->makeItem('dropdown', compact('content', 'attributes'), $callback); |
135
|
15 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Add a divider item to the menu. |
139
|
|
|
*/ |
140
|
15 |
|
public function divider() |
141
|
|
|
{ |
142
|
15 |
|
$this->makeItem('divider'); |
143
|
15 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Add a header item to the menu. |
147
|
|
|
* |
148
|
|
|
* @param string $content |
149
|
|
|
*/ |
150
|
15 |
|
public function header($content) |
151
|
|
|
{ |
152
|
15 |
|
$this->makeItem('header', compact('content')); |
153
|
15 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Add an item to the menu. |
157
|
|
|
* |
158
|
|
|
* @param array $properties |
159
|
|
|
* @param \Closure $callback |
160
|
|
|
*/ |
161
|
10 |
|
public function add($properties, Closure $callback = null) |
162
|
|
|
{ |
163
|
10 |
|
$this->makeItem('mixed', $properties, $callback); |
164
|
10 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get all the menu items. |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
10 |
|
public function all() |
172
|
|
|
{ |
173
|
10 |
|
return $this->items->all(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the menu items count. |
178
|
|
|
* |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
15 |
|
public function count() |
182
|
|
|
{ |
183
|
15 |
|
return $this->items->count(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/* ------------------------------------------------------------------------------------------------ |
187
|
|
|
| Check Functions |
188
|
|
|
| ------------------------------------------------------------------------------------------------ |
189
|
|
|
*/ |
190
|
|
|
/** |
191
|
|
|
* Check if menu has items. |
192
|
|
|
* |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
10 |
|
public function hasItems() |
196
|
|
|
{ |
197
|
10 |
|
return ! $this->items->isEmpty(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/* ------------------------------------------------------------------------------------------------ |
201
|
|
|
| Other Functions |
202
|
|
|
| ------------------------------------------------------------------------------------------------ |
203
|
|
|
*/ |
204
|
|
|
/** |
205
|
|
|
* Make an parent item and add it to the menu. |
206
|
|
|
* |
207
|
|
|
* @param string $type |
208
|
|
|
* @param array $properties |
209
|
|
|
* @param Closure|null $callback |
210
|
|
|
* |
211
|
|
|
* @return MenuItem |
212
|
|
|
*/ |
213
|
25 |
|
private function makeItem($type, $properties = [], Closure $callback = null) |
214
|
|
|
{ |
215
|
25 |
|
$properties = array_merge($properties, [ |
216
|
25 |
|
'type' => $type, |
217
|
25 |
|
'root' => true, |
218
|
25 |
|
]); |
219
|
|
|
|
220
|
25 |
|
$item = MenuItem::make($properties, $callback); |
221
|
25 |
|
$this->addItem($item); |
222
|
|
|
|
223
|
25 |
|
return $item; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Add an item to the menu. |
228
|
|
|
* |
229
|
|
|
* @param MenuItem $item |
230
|
|
|
*/ |
231
|
25 |
|
private function addItem(MenuItem $item) |
232
|
|
|
{ |
233
|
25 |
|
$this->items->push($item); |
234
|
25 |
|
} |
235
|
|
|
} |
236
|
|
|
|