-
Notifications
You must be signed in to change notification settings - Fork 2
/
BreadcrumbPath.php
200 lines (169 loc) · 3.99 KB
/
BreadcrumbPath.php
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
<?php
namespace Bundle\BreadcrumbBundle;
use Symfony\Component\Routing\Router;
class BreadcrumbPath
{
/**
* Properties on this breadcrumb link
*/
protected
$name = null, // the name of the breadcrumb link
$route = null, // the route of the breadcrumb link
$attributes = null, // an array of attributes for the li
$options = null; // an array of options
/**
* Metadata on this breadcrumb item
*/
protected
$parent = null; // parent BreadcrumbItem
/**
* Class constructor
*
* @param string $name The name of this link
* @param string $route The route for this link to use.
* @param array $attributes Attributes to place on the li tag of this breadcrumb link
*/
public function __construct($name, $route, $attributes = array(), $options = array())
{
$this->name = $name;
$this->route = $route;
$this->attributes = $attributes;
$this->options = $options;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getRoute()
{
return $this->route;
}
public function getParamConverter()
{
$paramConverter = $this->getOption('param_converter');
return $this->get('request')->attributes->get($paramConverter);
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param string $id The id of the attribute to return
*
* @return mixed
*/
public function getAttribute($id)
{
if ($this->hasAttribute($id)) {
return $this->attributes[$id];
}
}
/**
* @param string $id The id of the attribute
*
* @return boolean
*/
public function hasAttribute($id)
{
return isset($this->attributes[$id]);
}
public function setAttribute($id, $value)
{
$this->attributes[$id] = $value;
}
/**
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* @param string $id The id of the option to return
*
* @return mixed
*/
public function getOption($id)
{
if ($this->hasOption($id)) {
return $this->options[$id];
}
}
/**
* @param string $id The id of the option
*
* @return boolean
*/
public function hasOption($id)
{
return isset($this->options[$id]);
}
public function setOptions($id, $value)
{
$this->options[$id] = $value;
}
public function setParent($parent)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
public function get($id)
{
return $this->getParent()->getParent()->getContainer()->get($id);
}
/**
* Renders the anchor tag for this breadcrumb link.
*
* If no route is specified, the name will be output.
*
* @return string
*/
public function renderLink()
{
$name = $this->renderName();
$route = $this->getRoute();
if (!$route) {
return $name;
}
$router = $this->get('router');
// if a param converter is defined
if ($this->hasOption('param_converter')) {
$request = $this->get('request');
$object = $this->getParamConverter();
$params = $request->attributes->all();
unset($params['_controller']);
unset($params['_route']);
unset($params[$this->getOption('param_converter')]);
return sprintf('<a href="%s">%s</a>', $router->generate($route, $params), $object);
}
return sprintf('<a href="%s">%s</a>', $router->generate($route), $name);
}
/**
* Renders the name of this breadcrumb link
*
* @return string
*/
public function renderName()
{
if ($this->hasOption('param_converter') && null === $this->getName()) {
$name = $this->getParamConverter();
}
return $this->getName();
}
}