/
MenuDecorator.php
125 lines (108 loc) · 3.55 KB
/
MenuDecorator.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
<?php
/*
* This file is part of the MopaBootstrapBundle.
*
* (c) Philipp A. Mohrenweiser <phiamo@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mopa\Bundle\BootstrapBundle\Menu\Factory;
use Knp\Menu\ItemInterface;
/**
* Decorator for integrating Bootstrap Menus into KnpMenu.
*/
class MenuDecorator
{
/**
* Builds a menu item based.
*
* @param ItemInterface $item
* @param array $options
*/
public function buildItem(ItemInterface $item, array $options)
{
if ($options['navbar']) {
$item->setChildrenAttribute('class', 'nav navbar-nav'.($options['navbar-right']?' navbar-right':''));
}
if ($options['pills']) {
$item->setChildrenAttribute('class', 'nav nav-pills');
}
if ($options['stacked']) {
$class = $item->getChildrenAttribute('class');
$item->setChildrenAttribute('class', $class.' nav-stacked');
}
if ($options['dropdown-header']) {
$item
->setAttribute('role', 'presentation')
->setAttribute('class', 'dropdown-header')
->setUri(null);
}
if ($options['list-group']) {
$item->setChildrenAttribute('class', 'list-group');
$item->setAttribute('class', 'list-group-item');
}
if ($options['list-group-item']) {
$item->setAttribute('class', 'list-group-item');
}
if ($options['dropdown']) {
$item
->setUri('#')
->setAttribute('class', trim('dropdown '.$item->getAttribute('class')))
->setLinkAttribute('class', 'dropdown-toggle')
->setLinkAttribute('data-toggle', 'dropdown')
->setChildrenAttribute('class', 'dropdown-menu')
->setChildrenAttribute('role', 'menu');
if ($options['caret']) {
$item->setExtra('caret', 'true');
}
}
if ($options['divider']) {
$item
->setLabel('')
->setUri(null)
->setAttribute('role', 'presentation')
->setAttribute('class', 'divider');
}
if ($options['pull-right']) {
$className = $options['navbar'] ? 'navbar-right' : 'pull-right';
$class = $item->getChildrenAttribute('class', '');
$item->setChildrenAttribute('class', $class.' '.$className);
}
if ($options['icon']) {
$item->setExtra('icon', $options['icon']);
}
if ($options['badge']) {
$item->setExtra('badge', $options['badge']);
}
if ($options['badge-class']) {
$item->setExtra('badge_class', $options['badge-class']);
}
}
/**
* Builds the options for extension.
*
* @param array $options
*
* @return array $options
*/
public function buildOptions(array $options)
{
return array_merge(array(
'navbar' => false,
'navbar-right' => false,
'pills' => false,
'stacked' => false,
'dropdown-header' => false,
'dropdown' => false,
'list-group' => false,
'list-group-item' => false,
'caret' => false,
'pull-right' => false,
'icon' => false,
'divider' => false,
'badge' => false,
'badge-class' => false,
), $options);
}
}