1
|
|
|
<?php namespace Arcanesoft\Sidebar; |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
4
|
|
|
use Illuminate\Support\Arr; |
5
|
|
|
use Illuminate\Support\HtmlString; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class SidebarItem |
9
|
|
|
* |
10
|
|
|
* @package Arcanesoft\Sidebar |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class SidebarItem implements Arrayable |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
public $title; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
public $url; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
public $icon; |
31
|
|
|
|
32
|
|
|
/** @var \Arcanesoft\Sidebar\SidebarCollection */ |
33
|
|
|
public $children; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
protected $roles; |
37
|
|
|
|
38
|
|
|
/** @var array */ |
39
|
|
|
protected $permissions; |
40
|
|
|
|
41
|
|
|
/** @var boolean */ |
42
|
|
|
private $selected; |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Constructor |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* SidebarItem constructor. |
51
|
|
|
* |
52
|
|
|
* @param array $attributes |
53
|
|
|
*/ |
54
|
16 |
|
public function __construct(array $attributes) |
55
|
|
|
{ |
56
|
16 |
|
$this->name = Arr::pull($attributes, 'name'); |
57
|
16 |
|
$this->setTitle(Arr::pull($attributes, 'title')); |
58
|
16 |
|
$this->icon = Arr::pull($attributes, 'icon'); |
59
|
16 |
|
$this->roles = Arr::pull($attributes, 'roles', []); |
|
|
|
|
60
|
16 |
|
$this->permissions = Arr::pull($attributes, 'permissions', []); |
|
|
|
|
61
|
16 |
|
$this->children = (new SidebarCollection)->pushSidebarItems( |
62
|
16 |
|
Arr::pull($attributes, 'children', []) |
63
|
|
|
); |
64
|
16 |
|
$this->selected = false; |
65
|
|
|
|
66
|
16 |
|
$this->parseUrl($attributes); |
67
|
16 |
|
} |
68
|
|
|
|
69
|
|
|
/* ----------------------------------------------------------------- |
70
|
|
|
| Setters & Getters |
71
|
|
|
| ----------------------------------------------------------------- |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the title. |
76
|
|
|
* |
77
|
|
|
* @param string $title |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
16 |
|
public function setTitle(string $title) |
82
|
|
|
{ |
83
|
16 |
|
$this->title = trans()->has($title) |
84
|
|
|
? trans()->get($title) |
85
|
16 |
|
: $title; |
86
|
|
|
|
87
|
16 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the url. |
92
|
|
|
* |
93
|
|
|
* @param string $url |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
16 |
|
public function setUrl(string $url) |
98
|
|
|
{ |
99
|
16 |
|
$this->url = $url; |
100
|
|
|
|
101
|
16 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the selected item. |
106
|
|
|
* |
107
|
|
|
* @param string $name |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
4 |
|
public function setSelected(string $name) |
112
|
|
|
{ |
113
|
4 |
|
$this->selected = ($this->name === $name); |
114
|
4 |
|
$this->children->setSelected($name); |
115
|
|
|
|
116
|
4 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/* ----------------------------------------------------------------- |
120
|
|
|
| Main Methods |
121
|
|
|
| ----------------------------------------------------------------- |
122
|
|
|
*/ |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the url from the route. |
126
|
|
|
* |
127
|
|
|
* @param string $name |
128
|
|
|
* @param array $params |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function route($name, array $params = []) |
133
|
|
|
{ |
134
|
|
|
return $this->setUrl( |
135
|
|
|
route($name, $params) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set the url from the action. |
141
|
|
|
* |
142
|
|
|
* @param string|array $name |
143
|
|
|
* @param array $params |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function action($name, array $params = []) |
148
|
|
|
{ |
149
|
|
|
return $this->setUrl( |
150
|
|
|
action($name, $params) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the icon. |
156
|
|
|
* |
157
|
|
|
* @param string $classes |
158
|
|
|
* |
159
|
|
|
* @return \Illuminate\Support\HtmlString |
160
|
|
|
*/ |
161
|
|
|
public function icon($classes = '') : HtmlString |
162
|
|
|
{ |
163
|
|
|
$html = $this->icon |
164
|
|
|
? '<i class="' . $this->icon . ' ' . $classes . '"></i>' |
165
|
|
|
: ''; |
166
|
|
|
|
167
|
|
|
return new HtmlString($html); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the active/inactive class. |
172
|
|
|
* |
173
|
|
|
* @param string $active |
174
|
|
|
* @param string $inactive |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function active($active = 'active', $inactive = ''): string |
179
|
|
|
{ |
180
|
|
|
return $this->isActive() ? $active : $inactive; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/* ----------------------------------------------------------------- |
184
|
|
|
| Check Methods |
185
|
|
|
| ----------------------------------------------------------------- |
186
|
|
|
*/ |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Check if has children. |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function hasChildren(): bool |
194
|
|
|
{ |
195
|
|
|
return $this->children->isNotEmpty(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check if the item is active. |
200
|
|
|
* |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
12 |
|
public function isActive() : bool |
204
|
|
|
{ |
205
|
12 |
|
return $this->isSelected() || $this->children->hasAnySelected(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Check if the item is selected. |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
12 |
|
public function isSelected() : bool |
214
|
|
|
{ |
215
|
12 |
|
return $this->selected; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/* ----------------------------------------------------------------- |
219
|
|
|
| Other Methods |
220
|
|
|
| ----------------------------------------------------------------- |
221
|
|
|
*/ |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Parse the url attribute. |
225
|
|
|
* |
226
|
|
|
* @param array $attributes |
227
|
|
|
* |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
16 |
|
protected function parseUrl(array $attributes) : void |
231
|
|
|
{ |
232
|
16 |
|
if (isset($attributes['url'])) |
233
|
16 |
|
$this->setUrl($attributes['url']); |
234
|
|
|
elseif (isset($attributes['route'])) |
235
|
|
|
$this->route(...Arr::wrap($attributes['route'])); |
|
|
|
|
236
|
|
|
elseif (isset($attributes['action'])) |
237
|
|
|
$this->action(...Arr::wrap($attributes['action'])); |
238
|
|
|
else |
239
|
|
|
$this->setUrl('#'); |
240
|
16 |
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get the instance as an array. |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
12 |
|
public function toArray() |
248
|
|
|
{ |
249
|
|
|
return [ |
250
|
12 |
|
'name' => $this->name, |
251
|
12 |
|
'title' => $this->title, |
252
|
12 |
|
'url' => $this->url, |
253
|
12 |
|
'icon' => $this->icon, |
254
|
12 |
|
'active' => $this->isActive(), |
255
|
12 |
|
'children' => $this->children->toArray(), |
256
|
12 |
|
'roles' => $this->roles, |
257
|
12 |
|
'permissions' => $this->permissions, |
258
|
|
|
]; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..