-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
MenuItemModel.php
232 lines (194 loc) · 4.88 KB
/
MenuItemModel.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/*
* This file is part of the Tabler bundle, created by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace KevinPapst\TablerBundle\Model;
class MenuItemModel implements MenuItemInterface
{
private string $identifier;
private string $label;
private ?string $route;
/** @var array<string, mixed> */
private array $routeArgs;
private ?string $icon;
/** @var array<MenuItemInterface> */
private array $children = [];
private ?MenuItemInterface $parent = null;
private ?string $badge = null;
private ?string $badgeColor = null;
private bool $isActive = false;
private bool $divider = false;
private bool $expanded = false;
private string $translationDomain = 'messages';
/**
* @param array<string, mixed> $routeArgs
*/
public function __construct(
string $identifier,
string $label,
?string $route = null,
array $routeArgs = [],
?string $icon = null
) {
$this->identifier = $identifier;
$this->label = $label;
$this->route = $route;
$this->routeArgs = $routeArgs;
$this->icon = $icon;
}
/**
* @return array<MenuItemInterface>
*/
public function getChildren(): array
{
return $this->children;
}
/**
* @param array<MenuItemInterface> $children
*/
public function setChildren(array $children): void
{
$this->children = $children;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(string $icon): void
{
$this->icon = $icon;
}
public function getIdentifier(): string
{
return $this->identifier;
}
public function getIsActive(): bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): void
{
$this->parent?->setIsActive($isActive);
$this->isActive = $isActive;
}
public function hasParent(): bool
{
return $this->parent !== null;
}
public function getParent(): ?MenuItemInterface
{
return $this->parent;
}
public function setParent(MenuItemInterface $parent): void
{
$this->parent = $parent;
}
public function getLabel(): string
{
return $this->label;
}
public function setLabel(string $label): void
{
$this->label = $label;
}
public function getRoute(): ?string
{
return $this->route;
}
public function setRoute(?string $route): void
{
$this->route = $route;
}
public function getRouteArgs(): array
{
return $this->routeArgs;
}
/**
* @param array<string, mixed> $routeArgs
*/
public function setRouteArgs(array $routeArgs): void
{
$this->routeArgs = $routeArgs;
}
public function hasChildren(): bool
{
return \count($this->children) > 0;
}
public function addChild(MenuItemInterface $child): void
{
$child->setParent($this);
$this->children[] = $child;
}
public function removeChild(MenuItemInterface $child): void
{
if (false !== ($key = array_search($child, $this->children))) {
unset($this->children[$key]);
}
}
public function getActiveChild(): ?MenuItemInterface
{
foreach ($this->children as $child) {
if ($child->isActive()) {
return $child;
}
}
return null;
}
public function hasActiveChild(): bool
{
return null !== $this->getActiveChild();
}
public function isActive(): bool
{
return $this->isActive;
}
public function setBadge(?string $badge): void
{
$this->badge = $badge;
}
public function setBadgeColor(?string $badgeColor): void
{
$this->badgeColor = $badgeColor;
}
public function getBadge(): ?string
{
return $this->badge;
}
public function getBadgeColor(): ?string
{
return $this->badgeColor;
}
public function isDivider(): bool
{
return $this->divider;
}
public function setDivider(bool $divider): void
{
$this->divider = $divider;
}
public function isExpanded(): bool
{
return $this->expanded;
}
/**
* Allows to manually expand menus in vertical navigation.
*
* @param bool $expanded
* @return void
*/
public function setExpanded(bool $expanded): void
{
$this->expanded = $expanded;
}
public function getTranslationDomain(): string
{
return $this->translationDomain;
}
public function setTranslationDomain(string $translationDomain): void
{
$this->translationDomain = $translationDomain;
}
}