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