1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Breadcrumbs; |
6
|
|
|
|
7
|
|
|
use Arcanedev\Breadcrumbs\Contracts\Builder as BuilderContract; |
8
|
|
|
use Arcanedev\Breadcrumbs\Entities\BreadcrumbCollection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Builder |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Builder implements BuilderContract |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** @var array */ |
23
|
|
|
protected $callbacks = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Breadcrumbs collection. |
27
|
|
|
* |
28
|
|
|
* @var \Arcanedev\Breadcrumbs\Entities\BreadcrumbCollection |
29
|
|
|
*/ |
30
|
|
|
protected $breadcrumbs; |
31
|
|
|
|
32
|
|
|
/* ----------------------------------------------------------------- |
33
|
|
|
| Constructor |
34
|
|
|
| ----------------------------------------------------------------- |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create the builder instance. |
39
|
|
|
* |
40
|
|
|
* @param array $callbacks |
41
|
|
|
*/ |
42
|
48 |
|
public function __construct(array $callbacks = []) |
43
|
|
|
{ |
44
|
48 |
|
$this->breadcrumbs = new BreadcrumbCollection; |
45
|
48 |
|
$this->setCallbacks($callbacks); |
46
|
48 |
|
} |
47
|
|
|
|
48
|
|
|
/* ----------------------------------------------------------------- |
49
|
|
|
| Getters & Setters |
50
|
|
|
| ----------------------------------------------------------------- |
51
|
|
|
*/ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get breadcrumbs collection. |
55
|
|
|
* |
56
|
|
|
* @return \Arcanedev\Breadcrumbs\Entities\BreadcrumbCollection |
57
|
|
|
*/ |
58
|
6 |
|
public function get(): BreadcrumbCollection |
59
|
|
|
{ |
60
|
6 |
|
return $this->breadcrumbs; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get callbacks. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
6 |
|
public function getCallbacks(): array |
69
|
|
|
{ |
70
|
6 |
|
return $this->callbacks; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set callbacks. |
75
|
|
|
* |
76
|
|
|
* @param array $callbacks |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
48 |
|
private function setCallbacks(array $callbacks) |
81
|
|
|
{ |
82
|
48 |
|
$this->callbacks = $callbacks; |
83
|
|
|
|
84
|
48 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/* ----------------------------------------------------------------- |
88
|
|
|
| Main Methods |
89
|
|
|
| ----------------------------------------------------------------- |
90
|
|
|
*/ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Call breadcrumb. |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
* @param array $params |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
42 |
|
public function call($name, array $params = []) |
101
|
|
|
{ |
102
|
42 |
|
$this->checkName($name); |
103
|
|
|
|
104
|
30 |
|
array_unshift($params, $this); |
105
|
30 |
|
call_user_func_array($this->callbacks[$name], $params); |
106
|
|
|
|
107
|
30 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Call parent breadcrumb. |
112
|
|
|
* |
113
|
|
|
* @param string $name |
114
|
|
|
*/ |
115
|
12 |
|
public function parent($name): void |
116
|
|
|
{ |
117
|
12 |
|
$this->call($name, array_slice(func_get_args(), 1)); |
118
|
12 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Push a breadcrumb. |
122
|
|
|
* |
123
|
|
|
* @param string $title |
124
|
|
|
* @param string|null $url |
125
|
|
|
* @param array $data |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
30 |
|
public function push($title, $url = null, array $data = []) |
130
|
|
|
{ |
131
|
30 |
|
$this->breadcrumbs->addOne($title, $url, $data); |
132
|
|
|
|
133
|
30 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the breadcrumbs items as a plain array. |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
30 |
|
public function toArray(): array |
142
|
|
|
{ |
143
|
30 |
|
return $this->breadcrumbs->toArray(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/* ----------------------------------------------------------------- |
147
|
|
|
| Check Methods |
148
|
|
|
| ----------------------------------------------------------------- |
149
|
|
|
*/ |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check Name. |
153
|
|
|
* |
154
|
|
|
* @param string $name |
155
|
|
|
* |
156
|
|
|
* @throws \Arcanedev\Breadcrumbs\Exceptions\InvalidTypeException |
157
|
|
|
* @throws \Arcanedev\Breadcrumbs\Exceptions\InvalidCallbackNameException |
158
|
|
|
*/ |
159
|
42 |
|
private function checkName($name): void |
160
|
|
|
{ |
161
|
42 |
|
if ( ! is_string($name)) { |
162
|
6 |
|
throw new Exceptions\InvalidTypeException( |
163
|
6 |
|
'The name value must be a string, '.gettype($name).' given' |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
36 |
|
if ( ! isset($this->callbacks[$name])) { |
168
|
6 |
|
throw new Exceptions\InvalidCallbackNameException( |
169
|
6 |
|
"The callback name not found [{$name}]" |
170
|
|
|
); |
171
|
|
|
} |
172
|
30 |
|
} |
173
|
|
|
} |
174
|
|
|
|