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
|
|
|
public function __construct(FactoryInterface $factory, TranslatorInterface $translator) |
36
|
|
|
{ |
37
|
|
|
$this->factory = $factory; |
38
|
|
|
$this->translator = $translator; |
39
|
|
|
|
40
|
|
|
$this->defaultOptions = [ |
41
|
|
|
'attributes' => [ |
42
|
|
|
'class' => 'dropdown', |
43
|
|
|
], |
44
|
|
|
'childrenAttributes' => [ |
45
|
|
|
'class' => 'dropdown-menu', |
46
|
|
|
], |
47
|
|
|
'linkAttributes' => [ |
48
|
|
|
'class' => 'dropdown-toggle', |
49
|
|
|
'data-toggle' => 'dropdown', |
50
|
|
|
'data-target' => '#', |
51
|
|
|
], |
52
|
|
|
'extras' => [ |
53
|
|
|
'safe_label' => true, |
54
|
|
|
'translation_domain' => false, |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function buildMenu(array $menu, array $options): ItemInterface |
60
|
|
|
{ |
61
|
|
|
$menuOptions = array_merge_recursive([ |
62
|
|
|
'attributes' => [ |
63
|
|
|
'class' => 'nav', |
64
|
|
|
], |
65
|
|
|
'childrenAttributes' => [ |
66
|
|
|
'class' => 'nav nav-pills', |
67
|
|
|
], |
68
|
|
|
], $options); |
69
|
|
|
|
70
|
|
|
if (\array_key_exists('attributes', $menu)) { |
71
|
|
|
$menuOptions = array_merge($menuOptions, [ |
72
|
|
|
'childrenAttributes' => $menu['attributes'], |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$menuItem = $this->factory->createItem('main', $menuOptions); |
77
|
|
|
|
78
|
|
|
if (\array_key_exists('items', $menu)) { |
79
|
|
|
$this->buildSubMenu($menuItem, $menu['items']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $menuItem; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function buildSubMenu(ItemInterface $menu, array $configItems, array $baseMenuOptions = []): ItemInterface |
86
|
|
|
{ |
87
|
|
|
foreach ($configItems as $item) { |
88
|
|
|
$this->createItem($menu, $baseMenuOptions, $item); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $menu; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function trans(string $id, string $domain): string |
95
|
|
|
{ |
96
|
|
|
return $this->translator->trans($id, [], $domain); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function createItem(ItemInterface $menu, array $baseMenuOptions, array $itemDefinition): void |
100
|
|
|
{ |
101
|
|
|
$label = $this->createLabel($itemDefinition); |
102
|
|
|
|
103
|
|
|
$menuOptions = self::getOptions($baseMenuOptions, $itemDefinition); |
104
|
|
|
|
105
|
|
|
if (\array_key_exists('children', $itemDefinition) && \count($itemDefinition['children']) > 0) { |
106
|
|
|
$label .= ' <b class="caret caret-menu"></b>'; |
107
|
|
|
$menuOptions = array_merge($menuOptions, $this->defaultOptions, [ |
108
|
|
|
'label' => $label, |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$subMenu = $this->factory->createItem($label, $menuOptions); |
113
|
|
|
$menu->addChild($subMenu); |
114
|
|
|
|
115
|
|
|
if (\array_key_exists('children', $itemDefinition) && \count($itemDefinition['children']) > 0) { |
116
|
|
|
$this->buildSubMenu($subMenu, $itemDefinition['children']); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function createLabel(array $itemDefinition): string |
121
|
|
|
{ |
122
|
|
|
$label = $itemDefinition['label']; |
123
|
|
|
|
124
|
|
|
if (\array_key_exists('label_catalogue', $itemDefinition) && false !== $itemDefinition['label_catalogue']) { |
125
|
|
|
$label = $this->trans($itemDefinition['label'], $itemDefinition['label_catalogue']); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (\array_key_exists('icon', $itemDefinition) && \is_string($itemDefinition['icon'])) { |
129
|
|
|
$label = '<i class="'.$itemDefinition['icon'].'"></i> '.$label; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $label; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private static function getOptions(array $baseMenuOptions, array $itemDefinition): array |
136
|
|
|
{ |
137
|
|
|
$options = array_merge($baseMenuOptions, [ |
138
|
|
|
'route' => $itemDefinition['route'], |
139
|
|
|
'routeParameters' => $itemDefinition['routeParams'] ?? [], |
140
|
|
|
'linkAttributes' => [], |
141
|
|
|
'extras' => [ |
142
|
|
|
'safe_label' => true, |
143
|
|
|
'translation_domain' => false, |
144
|
|
|
], |
145
|
|
|
]); |
146
|
|
|
|
147
|
|
|
if (\array_key_exists('class', $itemDefinition)) { |
148
|
|
|
$options['linkAttributes']['class'] = $itemDefinition['class']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $options; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|