1
|
|
|
<?php namespace Arcanesoft\Sidebar; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Sidebar\Contracts\Manager as ManagerContract; |
4
|
|
|
use Illuminate\Support\Collection; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Manager |
8
|
|
|
* |
9
|
|
|
* @package Arcanesoft\Sidebar |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Manager implements ManagerContract |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Properties |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** @var \Arcanesoft\Sidebar\SidebarCollection */ |
20
|
|
|
protected $items; |
21
|
|
|
|
22
|
|
|
/** @var bool */ |
23
|
|
|
protected $shown = true; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Constructor |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
24 |
|
public function __construct() |
31
|
|
|
{ |
32
|
24 |
|
$this->items = new SidebarCollection; |
33
|
24 |
|
} |
34
|
|
|
|
35
|
|
|
/* ----------------------------------------------------------------- |
36
|
|
|
| Getters & Setters |
37
|
|
|
| ----------------------------------------------------------------- |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the selected item. |
42
|
|
|
* |
43
|
|
|
* @param string $name |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
4 |
|
public function setSelectedItem(string $name) |
48
|
|
|
{ |
49
|
4 |
|
$this->items->setSelected($name); |
50
|
|
|
|
51
|
4 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the sidebar items. |
56
|
|
|
* |
57
|
|
|
* @return \Arcanesoft\Sidebar\SidebarCollection |
58
|
|
|
*/ |
59
|
20 |
|
public function items() |
60
|
|
|
{ |
61
|
20 |
|
return $this->items; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/* ----------------------------------------------------------------- |
65
|
|
|
| Main Methods |
66
|
|
|
| ----------------------------------------------------------------- |
67
|
|
|
*/ |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Load the sidebar items from config files. |
71
|
|
|
* |
72
|
|
|
* @param array $items |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
4 |
|
public function loadFromArray(array $items) |
77
|
|
|
{ |
78
|
4 |
|
foreach ($items as $item) { |
79
|
4 |
|
if (is_array($item) && ! empty($item)) { |
80
|
4 |
|
$this->add($item); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Load sidebar items from config file(s). |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
8 |
|
public function loadFromConfig($key) |
95
|
|
|
{ |
96
|
8 |
|
$items = new Collection(config()->get($key, [])); |
97
|
|
|
|
98
|
8 |
|
if ($items->isEmpty()) |
99
|
|
|
return $this; |
100
|
|
|
|
101
|
|
|
$allHasConfig = $items->every(function ($item) { |
102
|
8 |
|
return config()->has($item); |
103
|
8 |
|
}); |
104
|
|
|
|
105
|
8 |
|
if ($allHasConfig) { |
106
|
|
|
$items->each(function ($item) { |
107
|
8 |
|
$this->loadFromConfig($item); |
108
|
8 |
|
}); |
109
|
|
|
} |
110
|
|
|
else { |
111
|
8 |
|
$this->add($items->toArray()); |
112
|
|
|
} |
113
|
|
|
|
114
|
8 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Add an item from array. |
119
|
|
|
* |
120
|
|
|
* @param array $item |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
16 |
|
public function add(array $item) |
125
|
|
|
{ |
126
|
16 |
|
$this->items->pushItem($item); |
127
|
|
|
|
128
|
16 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Show the sidebar. |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
4 |
|
public function show() |
137
|
|
|
{ |
138
|
4 |
|
$this->shown = true; |
139
|
|
|
|
140
|
4 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Hide the sidebar. |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
4 |
|
public function hide() |
149
|
|
|
{ |
150
|
4 |
|
$this->shown = false; |
151
|
|
|
|
152
|
4 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/* ----------------------------------------------------------------- |
156
|
|
|
| Check Methods |
157
|
|
|
| ----------------------------------------------------------------- |
158
|
|
|
*/ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Check if the sidebar has items. |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
12 |
|
public function hasItems() |
166
|
|
|
{ |
167
|
12 |
|
return $this->items->isNotEmpty(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Check if the sidebar is shown. |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
4 |
|
public function isShown() |
176
|
|
|
{ |
177
|
4 |
|
return $this->shown === true; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.