1
|
|
|
<?php namespace Arcanesoft\Sidebar; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Sidebar\Contracts\Manager as ManagerContract; |
4
|
|
|
use function foo\func; |
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Manager |
10
|
|
|
* |
11
|
|
|
* @package Arcanesoft\Sidebar |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Manager implements ManagerContract |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** @var \Arcanesoft\Foundation\Helpers\Sidebar\SidebarCollection */ |
23
|
|
|
protected $items; |
24
|
|
|
|
25
|
|
|
protected $shown = true; |
26
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Constructor |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
24 |
|
public function __construct() |
33
|
|
|
{ |
34
|
24 |
|
$this->items = new SidebarCollection; |
|
|
|
|
35
|
24 |
|
} |
36
|
|
|
|
37
|
|
|
/* ----------------------------------------------------------------- |
38
|
|
|
| Getters & Setters |
39
|
|
|
| ----------------------------------------------------------------- |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set the selected item. |
44
|
|
|
* |
45
|
|
|
* @param string $name |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
4 |
|
public function setSelectedItem(string $name) |
50
|
|
|
{ |
51
|
4 |
|
$this->items->setSelected($name); |
52
|
|
|
|
53
|
4 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the sidebar items. |
58
|
|
|
* |
59
|
|
|
* @return \Arcanesoft\Sidebar\SidebarCollection |
60
|
|
|
*/ |
61
|
20 |
|
public function items() |
62
|
|
|
{ |
63
|
20 |
|
return $this->items; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/* ----------------------------------------------------------------- |
67
|
|
|
| Main Methods |
68
|
|
|
| ----------------------------------------------------------------- |
69
|
|
|
*/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Load the sidebar items from config files. |
73
|
|
|
* |
74
|
|
|
* @param array $items |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
4 |
|
public function loadFromArray(array $items) |
79
|
|
|
{ |
80
|
4 |
|
foreach ($items as $item) { |
81
|
4 |
|
if (is_array($item) && ! empty($item)) { |
82
|
4 |
|
$this->add($item); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Load sidebar items from config file(s). |
91
|
|
|
* |
92
|
|
|
* @param string $key |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
8 |
|
public function loadFromConfig($key) |
97
|
|
|
{ |
98
|
8 |
|
$items = new Collection(config()->get($key, [])); |
99
|
|
|
|
100
|
8 |
|
if ($items->isEmpty()) |
101
|
|
|
return $this; |
102
|
|
|
|
103
|
|
|
$allHasConfig = $items->every(function ($item) { |
104
|
8 |
|
return config()->has($item); |
105
|
8 |
|
}); |
106
|
|
|
|
107
|
8 |
|
if ($allHasConfig) { |
108
|
|
|
$items->each(function ($item) { |
109
|
8 |
|
$this->loadFromConfig($item); |
110
|
8 |
|
}); |
111
|
|
|
} |
112
|
|
|
else { |
113
|
8 |
|
$this->add($items->toArray()); |
114
|
|
|
} |
115
|
|
|
|
116
|
8 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Add an item from array. |
121
|
|
|
* |
122
|
|
|
* @param array $item |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
16 |
|
public function add(array $item) |
127
|
|
|
{ |
128
|
16 |
|
$this->items->pushItem($item); |
129
|
|
|
|
130
|
16 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Show the sidebar. |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
4 |
|
public function show() |
139
|
|
|
{ |
140
|
4 |
|
$this->shown = true; |
141
|
|
|
|
142
|
4 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Hide the sidebar. |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
4 |
|
public function hide() |
151
|
|
|
{ |
152
|
4 |
|
$this->shown = false; |
153
|
|
|
|
154
|
4 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/* ----------------------------------------------------------------- |
158
|
|
|
| Check Methods |
159
|
|
|
| ----------------------------------------------------------------- |
160
|
|
|
*/ |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Check if the sidebar has items. |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
12 |
|
public function hasItems() |
168
|
|
|
{ |
169
|
12 |
|
return $this->items->isNotEmpty(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Check if the sidebar is shown. |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
4 |
|
public function isShown() |
178
|
|
|
{ |
179
|
4 |
|
return $this->shown === true; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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..