1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Braunstetter\MenuBundle\Items; |
6
|
|
|
|
7
|
|
|
use Braunstetter\Helper\Arr; |
8
|
|
|
use Braunstetter\MenuBundle\Contracts\MenuItemInterface; |
9
|
|
|
use Traversable; |
10
|
|
|
use Webmozart\Assert\Assert; |
11
|
|
|
|
12
|
|
|
trait MenuItemTrait |
13
|
|
|
{ |
14
|
|
|
public ?string $url = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array<int|string, bool|int|string> |
18
|
|
|
*/ |
19
|
|
|
public array $attr = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array<int|string, bool|int|string> |
23
|
|
|
*/ |
24
|
|
|
public array $linkAttr = []; |
25
|
|
|
|
26
|
|
|
private ?string $type = null; |
27
|
|
|
|
28
|
|
|
private ?string $label = null; |
29
|
|
|
|
30
|
|
|
private ?string $icon = null; |
31
|
|
|
|
32
|
|
|
private ?string $routeName = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array<string, int|string> |
36
|
|
|
*/ |
37
|
|
|
private ?array $routeParameters = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MenuItemInterface[]|iterable<MenuItemInterface> |
41
|
|
|
*/ |
42
|
|
|
private iterable $children = []; |
43
|
|
|
|
44
|
|
|
private bool $current = false; |
45
|
|
|
|
46
|
|
|
private bool $inActiveTrail = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array<string, mixed>|null $options |
50
|
|
|
*/ |
51
|
|
|
public function __construct(string $label, ?string $icon, ?array $options = []) |
52
|
|
|
{ |
53
|
|
|
$this->setLabel($label); |
54
|
|
|
$this->setIcon($icon); |
55
|
|
|
|
56
|
|
|
$attr = $options['attr'] ?? []; |
57
|
|
|
Assert::isArray($attr, 'The attr option must be an array, %s given.'); |
58
|
|
|
/** @var array<int|string, bool|int|string> $attr */ |
59
|
|
|
$this->setAttributes($attr); |
60
|
|
|
|
61
|
|
|
$linkAttr = $options['linkAttr'] ?? []; |
62
|
|
|
Assert::isArray($linkAttr, 'The linkAttr option must be an array, %s given.'); |
63
|
|
|
/** @var array<int|string, bool|int|string> $linkAttr */ |
64
|
|
|
$this->setLinkAttributes($linkAttr); |
65
|
|
|
|
66
|
|
|
if (isset($options['target'])) { |
67
|
|
|
$target = $options['target']; |
68
|
|
|
Assert::string($target, 'The target option must be a string, %s given.'); |
69
|
|
|
$this->setTarget($target); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getType(): ?string |
74
|
|
|
{ |
75
|
|
|
return $this->type; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function setType(string $type): static |
79
|
|
|
{ |
80
|
|
|
$this->type = $type; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getLabel(): ?string |
85
|
|
|
{ |
86
|
|
|
return $this->label; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setLabel(string $label): static |
90
|
|
|
{ |
91
|
|
|
$this->label = $label; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getIcon(): ?string |
96
|
|
|
{ |
97
|
|
|
return $this->icon; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setIcon(?string $icon): void |
101
|
|
|
{ |
102
|
|
|
$this->icon = $icon; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getRouteName(): ?string |
106
|
|
|
{ |
107
|
|
|
return $this->routeName ?? null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setRouteName(?string $routeName): static |
111
|
|
|
{ |
112
|
|
|
$this->routeName = $routeName; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array<string, string|int>|null |
118
|
|
|
*/ |
119
|
|
|
public function getRouteParameters(): ?array |
120
|
|
|
{ |
121
|
|
|
return $this->routeParameters; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setRouteParameter(string $parameterName, string|int $parameterValue): static |
125
|
|
|
{ |
126
|
|
|
$this->routeParameters[$parameterName] = $parameterValue; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array<string, string|int>|null $routeParameters |
132
|
|
|
*/ |
133
|
|
|
public function setRouteParameters(?array $routeParameters): static |
134
|
|
|
{ |
135
|
|
|
$this->routeParameters = $routeParameters; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return MenuItemInterface[]|iterable<MenuItemInterface> |
141
|
|
|
*/ |
142
|
|
|
public function getChildren(): iterable |
143
|
|
|
{ |
144
|
|
|
if ($this->children instanceof Traversable) { |
145
|
|
|
$this->children = iterator_to_array($this->children, false); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->children; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function setChildren(callable $children): static |
152
|
|
|
{ |
153
|
|
|
$traversableResult = call_user_func($children); |
154
|
|
|
Assert::isInstanceOf($traversableResult, Traversable::class, 'The children must be an instance of %s.'); |
155
|
|
|
$result = iterator_to_array($traversableResult); |
156
|
|
|
Assert::allIsInstanceOf($result, MenuItemInterface::class, 'All children must be an instance of %s.'); |
157
|
|
|
$this->children = $result; |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function hasChildren(): bool |
162
|
|
|
{ |
163
|
|
|
if ($this->children instanceof Traversable) { |
164
|
|
|
$this->children = iterator_to_array($this->children); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return ! empty($this->children); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function isCurrent(): bool |
171
|
|
|
{ |
172
|
|
|
return $this->current; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function setCurrent(bool $current): void |
176
|
|
|
{ |
177
|
|
|
$this->current = $current; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function isInActiveTrail(): bool |
181
|
|
|
{ |
182
|
|
|
return $this->inActiveTrail; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setInActiveTrail(bool $inActiveTrail): void |
186
|
|
|
{ |
187
|
|
|
$this->inActiveTrail = $inActiveTrail; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getUrl(): ?string |
191
|
|
|
{ |
192
|
|
|
return $this->url; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function setTarget(string $target): static |
196
|
|
|
{ |
197
|
|
|
$this->linkAttr = array_replace($this->linkAttr, [ |
198
|
|
|
'target' => $target, |
199
|
|
|
]); |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function addClass(string $class): void |
204
|
|
|
{ |
205
|
|
|
/** @var array<string, string|int|bool> $resultArray */ |
206
|
|
|
$resultArray = Arr::attachClass($this->attr, $class); |
207
|
|
|
$this->attr = $resultArray; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array<string|int, string|int|bool> $attr |
212
|
|
|
*/ |
213
|
|
|
public function setAttributes(array $attr): void |
214
|
|
|
{ |
215
|
|
|
foreach ($attr as $value) { |
216
|
|
|
Assert::oneOf(gettype($value), ['string', 'integer', 'boolean']); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$this->attr = $attr; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param array<string|int, string|int|bool> $attr |
224
|
|
|
*/ |
225
|
|
|
public function setLinkAttributes(array $attr): void |
226
|
|
|
{ |
227
|
|
|
foreach ($attr as $value) { |
228
|
|
|
Assert::oneOf(gettype($value), ['string', 'integer', 'boolean']); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$this->linkAttr = $attr; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return array<int|string, bool|int|string> |
236
|
|
|
*/ |
237
|
|
|
public function getAttr(): array |
238
|
|
|
{ |
239
|
|
|
return $this->attr; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return array<int|string, bool|int|string> |
244
|
|
|
*/ |
245
|
|
|
public function getLinkAttr(): array |
246
|
|
|
{ |
247
|
|
|
return $this->linkAttr; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|