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