-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathBreadcrumbRenderer.php
More file actions
160 lines (136 loc) · 4.6 KB
/
BreadcrumbRenderer.php
File metadata and controls
160 lines (136 loc) · 4.6 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
<?php
namespace Knp\Menu\Renderer;
use Knp\Menu\ItemInterface;
/**
* Renders MenuItem tree as a breadcrumb
*/
class BreadcrumbRenderer extends Renderer implements RendererInterface
{
private $defaultOptions;
/**
* @param array $defaultOptions
* @param string $charset
*/
public function __construct(array $defaultOptions = array(), $charset = null)
{
$this->defaultOptions = array_merge(array(
'current_as_link' => true,
'current_class' => 'current',
'additional_path' => null,
'compressed' => false,
'allow_safe_labels' => false,
'root_attributes' => array(),
), $defaultOptions);
parent::__construct($charset);
}
/**
* Renders a menu with the specified renderer.
*
* @param \Knp\Menu\ItemInterface $item
* @param array $options
* @return string
*/
public function render(ItemInterface $item, array $options = array())
{
$options = array_merge($this->defaultOptions, $options);
$breadcrumb = $item->getBreadcrumbsArray($options['additional_path']);
if (empty($breadcrumb)) {
return '';
}
return $this->renderBreadcrumb($breadcrumb, $options);
}
/**
* Renders the breadcrumb
*
* @param array $breadcrumb
* @param array $options
* @return string
*/
protected function renderBreadcrumb(array $breadcrumb, array $options)
{
$html = $this->format('<ul'.$this->renderHtmlAttributes($options['root_attributes']).'>', 'ul', 0, $options);
$html .= $this->renderList($breadcrumb, $options);
$html .= $this->format('</ul>', 'ul', 0, $options);
return $html;
}
/**
* Renders the breadcrumb list
*
* @param array $breadcrumb
* @param array $options
* @return string
*/
protected function renderList(array $breadcrumb, array $options)
{
$html = '';
foreach ($breadcrumb as $element) {
$element = array_replace(array('label' => null, 'uri' => null, 'item' => null), $element);
$html .= $this->renderItem($element['label'], $element['uri'], $options, $element['item']);
}
return $html;
}
/**
* @param string $label
* @param string $uri
* @param array $options
* @param \Knp\Menu\ItemInterface|null $item
* @return string
*/
protected function renderItem($label, $uri, array $options, ItemInterface $item = null)
{
$isCurrent = null !== $item && $item->isCurrent();
$attributes = $isCurrent ? array('class' => $options['current_class']) : array();
// opening li tag
$html = $this->format('<li'.$this->renderHtmlAttributes($attributes).'>', 'li', 1, $options);
// render the text/link inside the li tag
if (null === $uri || ($isCurrent && !$options['current_as_link'])) {
$content = $this->renderLabel($label, $options, $item);
} else {
$content = sprintf('<a href="%s">%s</a>', $this->escape($uri), $this->renderLabel($label, $options, $item));
}
$html .= $this->format($content, 'link', 1, $options);
// closing li tag
$html .= $this->format('</li>', 'li', 1, $options);
return $html;
}
/**
* @param string $label
* @param array $options
* @param \Knp\Menu\ItemInterface|null $item
* @return string
*/
protected function renderLabel($label, array $options, ItemInterface $item = null)
{
if ($options['allow_safe_labels'] && null !== $item && $item->getExtra('safe_label', false)) {
return $item->getLabel();
}
return $this->escape($label);
}
/**
* If $this->renderCompressed is on, this will apply the necessary
* spacing and line-breaking so that the particular thing being rendered
* makes up its part in a fully-rendered and spaced menu.
*
* @param string $html The html to render in an (un)formatted way
* @param string $type The type [ul,link,li] of thing being rendered
* @param integer $level
* @param array $options
* @return string
*/
protected function format($html, $type, $level, array $options)
{
if ($options['compressed']) {
return $html;
}
switch ($type) {
case 'ul':
case 'link':
$spacing = $level * 4;
break;
case 'li':
$spacing = $level * 4 - 2;
break;
}
return str_repeat(' ', $spacing).$html."\n";
}
}