|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Mark |
|
5
|
|
|
* Date: 14/09/2016 |
|
6
|
|
|
* Time: 16:13. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Classes; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Breadcrumbs. |
|
13
|
|
|
*/ |
|
14
|
|
|
class OldBreadcrumbs |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Stored crumbs are placed in an array for use on blade template. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $crumbs = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Removed crumbs from array for whatever reason. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $removed = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Specify characters to find from urls or arrays and replace with. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $characters = ['find' => ['_', '-'], 'replace' => [' ', ' ']]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Convert a url stirng to a usable breadcrumb object array. |
|
39
|
|
|
* |
|
40
|
|
|
* @param $string |
|
41
|
|
|
* @return $this |
|
42
|
|
|
* @throws \Exception |
|
43
|
|
|
*/ |
|
44
|
|
|
public function parseUrlAsCrumbs($string) |
|
45
|
|
|
{ |
|
46
|
|
|
$parsed_url = parse_url($string); |
|
47
|
|
|
|
|
48
|
|
|
$base_url = ($parsed_url['scheme'].'://'.$parsed_url['host']); |
|
49
|
|
|
|
|
50
|
|
|
if ($this->crumbs() == null) { |
|
51
|
|
|
$this->validateUrl($string); |
|
52
|
|
|
|
|
53
|
|
|
$this->addCrumb(new Breadcrumb('Home', $base_url)); |
|
54
|
|
|
|
|
55
|
|
|
$sublinks = array_filter(explode('/', parse_url($string, PHP_URL_PATH))); |
|
56
|
|
|
|
|
57
|
|
|
$lastLink = end($sublinks); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($sublinks as $link) { |
|
60
|
|
|
$url = ($base_url .= '/'.strtolower($link)); |
|
61
|
|
|
$title = ucwords(str_replace($this->characters['find'], $this->characters['replace'], $link)); |
|
62
|
|
|
|
|
63
|
|
|
if ($link == $lastLink) { |
|
64
|
|
|
$this->addCrumb(new Breadcrumb($title)); |
|
65
|
|
|
} else { |
|
66
|
|
|
$this->addCrumb(new Breadcrumb($title, $url)); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
throw new \Exception('You have already passed crumbs to this object for usage.'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Gets the current url for the user and applies it to a crumb. |
|
78
|
|
|
* |
|
79
|
|
|
* @return Breadcrumbs |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function fromCurrentUrl() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->parseUrlAsCrumbs(url()->current()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Enter a url that is an addition to the current domain. |
|
88
|
|
|
* |
|
89
|
|
|
* @param $string |
|
90
|
|
|
* @return Breadcrumbs |
|
|
|
|
|
|
91
|
|
|
*/ |
|
92
|
|
|
public function fromRelativeUrl($string) |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->parseUrlAsCrumbs(url($string)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* A user entered domain name from scratch. |
|
99
|
|
|
* |
|
100
|
|
|
* @param $string |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
public function fromAbsoluteUrl($string) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->parseUrlAsCrumbs($string); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Validate a url string is a valid url string. |
|
110
|
|
|
* |
|
111
|
|
|
* @param $string |
|
112
|
|
|
* @return $this |
|
113
|
|
|
* @throws \Exception |
|
114
|
|
|
*/ |
|
115
|
|
|
private function validateUrl($string) |
|
116
|
|
|
{ |
|
117
|
|
|
if (gethostbyname($string)) { |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
throw new \Exception('The string passed was not a valid domain to use'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Count the current crumbs that have been stored. |
|
126
|
|
|
* |
|
127
|
|
|
* @return int |
|
128
|
|
|
*/ |
|
129
|
|
|
public function count() |
|
130
|
|
|
{ |
|
131
|
|
|
return count($this->crumbs); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Return the objects crumbs that have been stored. |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
public function crumbs() |
|
140
|
|
|
{ |
|
141
|
|
|
foreach ($this->removed as $crumb) { |
|
142
|
|
|
$this->removeCrumb($crumb); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->crumbs; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Define if home should exist, this is bound to the preference settings |
|
150
|
|
|
* as of right now. |
|
151
|
|
|
* |
|
152
|
|
|
* @param $boolean |
|
153
|
|
|
* @return $this |
|
154
|
|
|
* @throws \Exception |
|
155
|
|
|
* @deprecated |
|
156
|
|
|
*/ |
|
157
|
|
|
public function homeExists($boolean) |
|
158
|
|
|
{ |
|
159
|
|
|
if (is_bool($boolean)) { |
|
160
|
|
|
if ($boolean == false) { |
|
|
|
|
|
|
161
|
|
|
$this->removed['Home'] = $this->crumb('home'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
throw new \Exception('Property type is not of array value.'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Remove a crumb from the object array. |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $crumbs |
|
174
|
|
|
* @return $this |
|
175
|
|
|
* @throws \Exception |
|
176
|
|
|
*/ |
|
177
|
|
|
public function remove($crumbs = []) |
|
178
|
|
|
{ |
|
179
|
|
|
if (is_array($crumbs)) { |
|
180
|
|
|
foreach ($crumbs as $name) { |
|
181
|
|
|
$this->removed[ucfirst($name)] = $this->crumb($name); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
throw new \Exception('Property is not of type array'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Alter a crumbs title to something else. |
|
192
|
|
|
* |
|
193
|
|
|
* @param array $crumbs |
|
194
|
|
|
* @return $this |
|
195
|
|
|
*/ |
|
196
|
|
|
public function rename($crumbs = []) |
|
197
|
|
|
{ |
|
198
|
|
|
foreach ($crumbs as $key => $value) { |
|
199
|
|
|
if ($this->keyExists($key)) { |
|
200
|
|
|
$this->changeCrumbTitle($this->crumb($key), $value); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Check if the breadcrumb array contains a crumb name. |
|
209
|
|
|
* |
|
210
|
|
|
* @param $string |
|
211
|
|
|
* @param null $array_key |
|
212
|
|
|
* @return bool |
|
213
|
|
|
*/ |
|
214
|
|
|
public function contain($string, $array_key = null) |
|
|
|
|
|
|
215
|
|
|
{ |
|
216
|
|
|
if ($array_key == null) { |
|
217
|
|
|
return $this->keyExists($string, $this->crumbs); |
|
218
|
|
|
} else { |
|
219
|
|
|
return $this->keyExists($string, $this->sliceKey($array_key)); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
private function keyExists($key, $array = []) |
|
224
|
|
|
{ |
|
225
|
|
|
if (empty($array)) { |
|
226
|
|
|
return array_key_exists(ucfirst($key), $this->crumbs); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return array_key_exists(ucfirst($key), $array); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
private function sliceKey($key) |
|
233
|
|
|
{ |
|
234
|
|
|
return array_slice($this->crumbs, ($key - 1), 1); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Does this object has a home property allowed. |
|
239
|
|
|
* |
|
240
|
|
|
* @return bool |
|
241
|
|
|
*/ |
|
242
|
|
|
public function hasHome() |
|
243
|
|
|
{ |
|
244
|
|
|
return ! $this->keyExists('home', $this->removed); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Return a crumb by its key name from the object. |
|
249
|
|
|
* |
|
250
|
|
|
* @param $name |
|
251
|
|
|
* @return mixed |
|
252
|
|
|
* @throws \Exception |
|
253
|
|
|
*/ |
|
254
|
|
|
private function crumb($name) |
|
255
|
|
|
{ |
|
256
|
|
|
if ($this->keyExists($name)) { |
|
257
|
|
|
return $this->crumbs[ucfirst($name)]; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
throw new \Exception('Crumb name does not exist in the array.'); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Change the title of a crumb. |
|
265
|
|
|
* |
|
266
|
|
|
* @param Breadcrumb $breadcrumb |
|
267
|
|
|
* @param $string |
|
268
|
|
|
* @return Breadcrumb |
|
269
|
|
|
*/ |
|
270
|
|
|
private function changeCrumbTitle(Breadcrumb $breadcrumb, $string) |
|
271
|
|
|
{ |
|
272
|
|
|
return $breadcrumb->setTitle($string); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Add a new breadcrumb object to the crumbs array. |
|
277
|
|
|
* |
|
278
|
|
|
* @param Breadcrumb $breadcrumb |
|
279
|
|
|
* @return Breadcrumb |
|
280
|
|
|
*/ |
|
281
|
|
|
private function addCrumb(Breadcrumb $breadcrumb) |
|
282
|
|
|
{ |
|
283
|
|
|
return $this->crumbs[$breadcrumb->title()] = $breadcrumb; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Remove breadcrumb object from the crumbs array. |
|
288
|
|
|
* |
|
289
|
|
|
* @param Breadcrumb $breadcrumb |
|
290
|
|
|
* @param bool $boolean |
|
291
|
|
|
* @return $this |
|
292
|
|
|
*/ |
|
293
|
|
|
private function removeCrumb(Breadcrumb $breadcrumb, $boolean = true) |
|
294
|
|
|
{ |
|
295
|
|
|
if ($boolean == true) { |
|
|
|
|
|
|
296
|
|
|
unset($this->crumbs[$breadcrumb->title()]); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
return $this; |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Class Breadcrumb. |
|
305
|
|
|
* |
|
306
|
|
|
* Singular objects for the breadcrumbs array. |
|
307
|
|
|
*/ |
|
308
|
|
|
class OldBreadcrumb |
|
|
|
|
|
|
309
|
|
|
{ |
|
310
|
|
|
/** |
|
311
|
|
|
* The name of the breadcrumb. |
|
312
|
|
|
* @var |
|
313
|
|
|
*/ |
|
314
|
|
|
private $title; |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* The url of the breadcrumb. |
|
318
|
|
|
* |
|
319
|
|
|
* @var |
|
320
|
|
|
*/ |
|
321
|
|
|
private $url; |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Create a new breadcrumb. |
|
325
|
|
|
* |
|
326
|
|
|
* Breadcrumb constructor. |
|
327
|
|
|
*/ |
|
328
|
|
|
public function __construct($title, $url = false) |
|
329
|
|
|
{ |
|
330
|
|
|
$this->title = $title; |
|
331
|
|
|
|
|
332
|
|
|
$this->url = $url; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Return breadcrumb title. |
|
337
|
|
|
* |
|
338
|
|
|
* @return mixed |
|
|
|
|
|
|
339
|
|
|
*/ |
|
340
|
|
|
public function title() |
|
341
|
|
|
{ |
|
342
|
|
|
return $this->title; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Return breadcrumb url link. |
|
347
|
|
|
* |
|
348
|
|
|
* @return null |
|
|
|
|
|
|
349
|
|
|
*/ |
|
350
|
|
|
public function url() |
|
351
|
|
|
{ |
|
352
|
|
|
return $this->url; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
public function setTitle($string) |
|
356
|
|
|
{ |
|
357
|
|
|
$this->title = ucfirst($string); |
|
358
|
|
|
|
|
359
|
|
|
return $this; |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|
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.