|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Marky |
|
5
|
|
|
* Date: 04/01/2018 |
|
6
|
|
|
* Time: 01:28. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Classes; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Http\Request; |
|
12
|
|
|
use Illuminate\Support\Collection; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Breadcrumbs. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Breadcrumbs |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* A collection of crumbs. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Collection |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $collection; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Breadcrumbs constructor. |
|
28
|
|
|
* @param Collection $collection |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Collection $collection) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->collection = $collection; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $title |
|
37
|
|
|
* @param string $path |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function addCrumb(string $title, string $path) |
|
41
|
|
|
{ |
|
42
|
|
|
$crumb = new \stdClass(); |
|
43
|
|
|
$crumb->title = $this->filter($title); |
|
44
|
|
|
$crumb->path = url($path); |
|
45
|
|
|
|
|
46
|
|
|
$this->collection->push($crumb); |
|
47
|
|
|
|
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $title |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function filter(string $title) |
|
56
|
|
|
{ |
|
57
|
|
|
return ucwords(str_replace(['_', '-'], [' ', ' '], $title)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return Collection |
|
62
|
|
|
*/ |
|
63
|
|
|
public function crumbs() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->collection; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* @param int $position |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function contain(string $name, int $position) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->collection->has($position) == false) { |
|
|
|
|
|
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->collection->get($position)->title == $this->filter($name); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param int $integer |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function hasCount(int $integer) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->collection->count() == $integer; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param int $count |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function limit(int $count) |
|
96
|
|
|
{ |
|
97
|
|
|
$collection = new Collection; |
|
98
|
|
|
|
|
99
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
100
|
|
|
if ($this->collection->has($i)) { |
|
101
|
|
|
$collection->push($this->collection->get($i)); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->collection = $collection; |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return Breadcrumbs |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function fromCurrentRoute() |
|
114
|
|
|
{ |
|
115
|
|
|
/** @var Breadcrumbs $instance */ |
|
116
|
|
|
$instance = app(self::class); |
|
117
|
|
|
|
|
118
|
|
|
/** @var array $routes */ |
|
119
|
|
|
$routes = explode('/', app(Request::class)->getRequestUri()); |
|
120
|
|
|
|
|
121
|
|
|
$urlPath = url(array_pull($routes, 0)); |
|
122
|
|
|
|
|
123
|
|
|
// Add the home array. |
|
124
|
|
|
$instance->addCrumb('Home', $urlPath); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
foreach ($routes as $route) { |
|
127
|
|
|
$urlPath = $urlPath.'/'.$route; |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$instance->addCrumb($route, $urlPath); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $instance; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.