-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathBreadcrumb.php
More file actions
226 lines (194 loc) · 5.9 KB
/
Breadcrumb.php
File metadata and controls
226 lines (194 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/*
* This file is part of the APYBreadcrumbTrailBundle.
*
* (c) Abhoryo <abhoryo@free.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace APY\BreadcrumbTrailBundle\Annotation;
/**
* @Annotation
*/
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class Breadcrumb
{
/**
* @var string Title of the breadcrumb
*/
private $title;
/**
* @var string The name of the route
*/
private $routeName;
/**
* @var mixed An array of parameters for the route
*/
private $routeParameters = [];
/**
* @var bool Whether to generate an absolute URL
*/
private $routeAbsolute = false;
/**
* @var int Position of the breadcrumb (default = 0)
*/
private $position = 0;
/**
* @var string Template of the breadcrumb trail
*/
private $template;
/**
* @var array with additional attributes for the breadcrumb
*/
private $attributes = [];
/**
* @param array|string|null $title title, or the legacy array that contains all annotation data. Passing `null` to reset the breadcrumb trail is deprecated and will throw an exception in `2.0`.
* @param ?string $routeName
* @param ?array<string,mixed> $routeParameters
* @param bool $routeAbsolute
* @param int $position
* @param ?string $template
* @param array $attributes
*/
public function __construct($title = null, $routeName = null, $routeParameters = null, $routeAbsolute = null, $position = null, $template = null, $attributes = null)
{
$data = [];
if (\is_string($title)) {
$data = ['title' => $title];
} elseif (\is_array($title)) {
$data = $title;
}
$data['routeName'] = $data['routeName'] ?? $routeName;
$data['routeParameters'] = $data['routeParameters'] ?? $routeParameters;
$data['routeAbsolute'] = $data['routeAbsolute'] ?? $routeAbsolute;
$data['position'] = $data['position'] ?? $position;
$data['template'] = $data['template'] ?? $template;
$data['attributes'] = $data['attributes'] ?? $attributes;
// When no data key is provided, the first value gets respected as the `value`
if (isset($data[0])) {
$data['value'] = $data[0];
unset($data[0]);
}
if (isset($data['value'])) {
$data['title'] = $data['value'];
unset($data['value']);
}
if (isset($data['route'])) {
if (\is_array($data['route'])) {
foreach ($data['route'] as $key => $value) {
$method = 'setRoute'.$key;
if (!method_exists($this, $method)) {
throw new \BadMethodCallException(\sprintf("Unknown property '%s' for the 'route' parameter on annotation '%s'.", $key, static::class));
}
$this->$method($value);
}
} else {
$data['routeName'] = $data['route'];
}
unset($data['route']);
}
foreach ($data as $key => $value) {
// Do not attempt setting values that were provided as null
if (null === $value) {
continue;
}
$method = 'set'.$key;
if (!method_exists($this, $method)) {
throw new \BadMethodCallException(\sprintf("Unknown property '%s' on annotation '%s'.", $key, static::class));
}
$this->$method($value);
}
}
/**
* Sets the title of the breadcrumb.
*
* @param string $title The title of the breadcrumb
*/
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
/**
* Sets the name of the route.
*
* @param string $routeName The name of the route
*/
public function setRouteName($routeName)
{
$this->routeName = $routeName;
}
public function getRouteName()
{
return $this->routeName;
}
/**
* Sets an array of parameters for the route.
*
* @param mixed $routeParameters An array of parameters for the route
*/
public function setRouteParameters($routeParameters)
{
$this->routeParameters = $routeParameters;
}
public function getRouteParameters()
{
return $this->routeParameters;
}
/**
* Whether to generate an absolute URL.
*
* @param bool $routeAbsolute Whether to generate an absolute URL
*/
public function setRouteAbsolute($routeAbsolute)
{
$this->routeAbsolute = $routeAbsolute;
}
public function getRouteAbsolute()
{
return $this->routeAbsolute;
}
/**
* Sets the position of the breadcrumb.
*
* @param int $position Position of the breadcrumb (default = 0)
*/
public function setPosition($position)
{
$this->position = $position;
}
public function getPosition()
{
return $this->position;
}
/**
* Sets the template of the breadcrumb trail.
*
* @param string $template with path of the breadcrumb trail that should get rendered
*/
public function setTemplate($template)
{
$this->template = $template;
}
public function getTemplate()
{
return $this->template;
}
/**
* Sets the additional attributes for the breadcrumb.
*
* @param array $attributes additional attributes for the breadcrumb
*/
public function setAttributes($attributes)
{
$this->attributes = $attributes;
}
public function getAttributes()
{
return $this->attributes;
}
}