1
|
|
|
<?php namespace Arcanedev\Breadcrumbs; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Breadcrumbs\Contracts\Breadcrumbs as BreadcrumbsContract; |
4
|
|
|
use Closure; |
5
|
|
|
use Illuminate\Support\HtmlString; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Breadcrumbs |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Breadcrumbs |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Breadcrumbs implements BreadcrumbsContract |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Constants |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
const DEFAULT_TEMPLATE = 'bootstrap-3'; |
21
|
|
|
|
22
|
|
|
/* ----------------------------------------------------------------- |
23
|
|
|
| Properties |
24
|
|
|
| ----------------------------------------------------------------- |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Default template view. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $template; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Supported template views. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $supported = [ |
40
|
|
|
'bootstrap-3' => 'breadcrumbs::bootstrap-3', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** @var array */ |
44
|
|
|
protected $callbacks = []; |
45
|
|
|
|
46
|
|
|
/* ----------------------------------------------------------------- |
47
|
|
|
| Constructor |
48
|
|
|
| ----------------------------------------------------------------- |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a Breadcrumbs instance. |
53
|
|
|
* |
54
|
|
|
* @param array $supported |
55
|
|
|
* @param string|null $template |
56
|
|
|
*/ |
57
|
20 |
|
public function __construct(array $supported, $template = null) |
58
|
|
|
{ |
59
|
20 |
|
$this->setSupported($supported); |
60
|
20 |
|
$this->setTemplate(is_null($template) ? self::DEFAULT_TEMPLATE : $template); |
61
|
20 |
|
} |
62
|
|
|
|
63
|
|
|
/* ----------------------------------------------------------------- |
64
|
|
|
| Getters & Setters |
65
|
|
|
| ----------------------------------------------------------------- |
66
|
|
|
*/ |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the supported template. |
70
|
|
|
* |
71
|
|
|
* @param array $supported |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
20 |
|
public function setSupported(array $supported) |
76
|
|
|
{ |
77
|
20 |
|
$this->supported = $supported; |
78
|
|
|
|
79
|
20 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set default template view. |
84
|
|
|
* |
85
|
|
|
* @param string $template |
86
|
|
|
* |
87
|
|
|
* @return self |
88
|
|
|
*/ |
89
|
20 |
|
public function setTemplate($template) |
90
|
|
|
{ |
91
|
20 |
|
$this->checkTemplate($template); |
92
|
|
|
|
93
|
20 |
|
$this->template = $template; |
94
|
|
|
|
95
|
20 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the template view. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
6 |
|
private function getView() |
104
|
|
|
{ |
105
|
6 |
|
return $this->supported[$this->template]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/* ----------------------------------------------------------------- |
109
|
|
|
| Main Methods |
110
|
|
|
| ----------------------------------------------------------------- |
111
|
|
|
*/ |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Register a breadcrumb domain. |
115
|
|
|
* |
116
|
|
|
* @param string $name |
117
|
|
|
* @param \Closure $callback |
118
|
|
|
* |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
20 |
|
public function register($name, Closure $callback) |
122
|
|
|
{ |
123
|
20 |
|
$this->checkCallbackName($name); |
124
|
|
|
|
125
|
20 |
|
$this->callbacks[$name] = $callback; |
126
|
|
|
|
127
|
20 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Render breadcrumbs items. |
132
|
|
|
* |
133
|
|
|
* @param string|null $name |
134
|
|
|
* @param array $params |
135
|
|
|
* |
136
|
|
|
* @return \Illuminate\Support\HtmlString |
137
|
|
|
*/ |
138
|
6 |
|
public function render($name = null, ...$params) |
139
|
|
|
{ |
140
|
6 |
|
return new HtmlString( |
141
|
6 |
|
view($this->getView(), [ |
142
|
6 |
|
'breadcrumbs' => $this->generate($name, $params) |
143
|
6 |
|
])->render() |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Generate the breadcrumbs. |
149
|
|
|
* |
150
|
|
|
* @param string $name |
151
|
|
|
* @param array $params |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
10 |
|
public function generate($name, ...$params) |
156
|
|
|
{ |
157
|
10 |
|
return (new Builder($this->callbacks)) |
158
|
10 |
|
->call($name, $params) |
159
|
10 |
|
->toArray(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/* ----------------------------------------------------------------- |
163
|
|
|
| Check Methods |
164
|
|
|
| ----------------------------------------------------------------- |
165
|
|
|
*/ |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Check Template. |
169
|
|
|
* |
170
|
|
|
* @param string $template |
171
|
|
|
* |
172
|
|
|
* @throws Exceptions\InvalidTemplateException |
173
|
|
|
* @throws Exceptions\InvalidTypeException |
174
|
|
|
*/ |
175
|
20 |
|
private function checkTemplate($template) |
176
|
|
|
{ |
177
|
20 |
|
if ( ! is_string($template)) { |
178
|
2 |
|
$type = gettype($template); |
179
|
2 |
|
throw new Exceptions\InvalidTypeException( |
180
|
2 |
|
"The default template name must be a string, $type given." |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
20 |
|
$template = strtolower(trim($template)); |
185
|
|
|
|
186
|
20 |
|
if ( ! array_key_exists($template, $this->supported)) { |
187
|
2 |
|
throw new Exceptions\InvalidTemplateException( |
188
|
2 |
|
"The template [$template] is not supported." |
189
|
|
|
); |
190
|
|
|
} |
191
|
20 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Check Name. |
195
|
|
|
* |
196
|
|
|
* @param string $name |
197
|
|
|
* |
198
|
|
|
* @throws Exceptions\InvalidTypeException |
199
|
|
|
*/ |
200
|
20 |
|
private function checkCallbackName(&$name) |
201
|
|
|
{ |
202
|
20 |
|
if ( ! is_string($name)) { |
203
|
2 |
|
$type = gettype($name); |
204
|
|
|
|
205
|
2 |
|
throw new Exceptions\InvalidTypeException( |
206
|
2 |
|
"The callback name value must be a string, $type given." |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
20 |
|
$name = strtolower(trim($name)); |
211
|
20 |
|
} |
212
|
|
|
} |
213
|
|
|
|