1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\MenuBundle\Menu; |
13
|
|
|
|
14
|
|
|
use Knp\Menu\FactoryInterface; |
15
|
|
|
use Knp\Menu\ItemInterface; |
16
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
17
|
|
|
|
18
|
|
|
final class ConfigBuilder implements ConfigBuilderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var FactoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $factory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TranslatorInterface |
27
|
|
|
*/ |
28
|
|
|
private $translator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $defaultOptions; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param FactoryInterface $factory |
37
|
|
|
* @param TranslatorInterface $translator |
38
|
|
|
*/ |
39
|
|
|
public function __construct(FactoryInterface $factory, TranslatorInterface $translator) |
40
|
|
|
{ |
41
|
|
|
$this->factory = $factory; |
42
|
|
|
$this->translator = $translator; |
43
|
|
|
|
44
|
|
|
$this->defaultOptions = [ |
45
|
|
|
'attributes' => [ |
46
|
|
|
'class' => 'dropdown', |
47
|
|
|
], |
48
|
|
|
'childrenAttributes' => [ |
49
|
|
|
'class' => 'dropdown-menu', |
50
|
|
|
], |
51
|
|
|
'linkAttributes' => [ |
52
|
|
|
'class' => 'dropdown-toggle', |
53
|
|
|
'data-toggle' => 'dropdown', |
54
|
|
|
'data-target' => '#', |
55
|
|
|
], |
56
|
|
|
'extras' => [ |
57
|
|
|
'safe_label' => true, |
58
|
|
|
'translation_domain' => false, |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function buildMenu(array $menu, array $options): ItemInterface |
67
|
|
|
{ |
68
|
|
|
$menuOptions = array_merge_recursive([ |
69
|
|
|
'attributes' => [ |
70
|
|
|
'class' => 'nav', |
71
|
|
|
], |
72
|
|
|
'childrenAttributes' => [ |
73
|
|
|
'class' => 'nav nav-pills', |
74
|
|
|
], |
75
|
|
|
], $options); |
76
|
|
|
|
77
|
|
|
if (\array_key_exists('attributes', $menu)) { |
78
|
|
|
$menuOptions = array_merge($menuOptions, [ |
79
|
|
|
'childrenAttributes' => $menu['attributes'], |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$menuItem = $this->factory->createItem('main', $menuOptions); |
84
|
|
|
|
85
|
|
|
if (\array_key_exists('items', $menu)) { |
86
|
|
|
$this->buildSubMenu($menuItem, $menu['items']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $menuItem; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ItemInterface $menu |
94
|
|
|
* @param array $configItems |
95
|
|
|
* @param array $baseMenuOptions |
96
|
|
|
* |
97
|
|
|
* @return ItemInterface |
98
|
|
|
*/ |
99
|
|
|
private function buildSubMenu(ItemInterface $menu, array $configItems, array $baseMenuOptions = []): ItemInterface |
100
|
|
|
{ |
101
|
|
|
foreach ($configItems as $item) { |
102
|
|
|
$this->createItem($menu, $baseMenuOptions, $item); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $menu; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $id |
110
|
|
|
* @param string $domain |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
private function trans(string $id, string $domain): string |
115
|
|
|
{ |
116
|
|
|
return $this->translator->trans($id, [], $domain); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param ItemInterface $menu |
121
|
|
|
* @param array $baseMenuOptions |
122
|
|
|
* @param array $itemDefinition |
123
|
|
|
*/ |
124
|
|
|
private function createItem(ItemInterface $menu, array $baseMenuOptions, array $itemDefinition): void |
125
|
|
|
{ |
126
|
|
|
$label = $this->createLabel($itemDefinition); |
127
|
|
|
|
128
|
|
|
$menuOptions = self::getOptions($baseMenuOptions, $itemDefinition); |
129
|
|
|
|
130
|
|
|
if (\array_key_exists('children', $itemDefinition) && \count($itemDefinition['children']) > 0) { |
131
|
|
|
$label .= ' <b class="caret caret-menu"></b>'; |
132
|
|
|
$menuOptions = array_merge($menuOptions, $this->defaultOptions, [ |
133
|
|
|
'label' => $label, |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$subMenu = $this->factory->createItem($label, $menuOptions); |
138
|
|
|
$menu->addChild($subMenu); |
139
|
|
|
|
140
|
|
|
if (\array_key_exists('children', $itemDefinition) && \count($itemDefinition['children']) > 0) { |
141
|
|
|
$this->buildSubMenu($subMenu, $itemDefinition['children']); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $itemDefinition |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
private function createLabel(array $itemDefinition): string |
151
|
|
|
{ |
152
|
|
|
$label = $itemDefinition['label']; |
153
|
|
|
|
154
|
|
|
if (\array_key_exists('label_catalogue', $itemDefinition) && false !== $itemDefinition['label_catalogue']) { |
155
|
|
|
$label = $this->trans($itemDefinition['label'], $itemDefinition['label_catalogue']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!empty($itemDefinition['icon'])) { |
159
|
|
|
$label = '<i class="'.$itemDefinition['icon'].'"></i> '.$label; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $label; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param array $baseMenuOptions |
167
|
|
|
* @param array $itemDefinition |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
private static function getOptions(array $baseMenuOptions, array $itemDefinition): array |
172
|
|
|
{ |
173
|
|
|
$options = array_merge($baseMenuOptions, [ |
174
|
|
|
'route' => $itemDefinition['route'], |
175
|
|
|
'routeParameters' => $itemDefinition['routeParams'] ?? [], |
176
|
|
|
'linkAttributes' => [], |
177
|
|
|
'extras' => [ |
178
|
|
|
'safe_label' => true, |
179
|
|
|
'translation_domain' => false, |
180
|
|
|
], |
181
|
|
|
]); |
182
|
|
|
|
183
|
|
|
if (\array_key_exists('class', $itemDefinition)) { |
184
|
|
|
$options['linkAttributes']['class'] = $itemDefinition['class']; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $options; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|