|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Braunstetter\MenuBundle\Items; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\String\UnicodeString; |
|
7
|
|
|
use Traversable; |
|
8
|
|
|
|
|
9
|
|
|
trait MenuItemTrait |
|
10
|
|
|
{ |
|
11
|
|
|
private string $type; |
|
12
|
|
|
private string $label; |
|
13
|
|
|
private ?string $icon; |
|
14
|
|
|
private ?string $routeName; |
|
15
|
|
|
private ?array $routeParameters; |
|
16
|
|
|
private array|Traversable $children; |
|
17
|
|
|
private bool $current; |
|
18
|
|
|
private bool $inActiveTrail; |
|
19
|
|
|
public string $handle; |
|
20
|
|
|
public string $url; |
|
21
|
|
|
public string $target; |
|
22
|
|
|
public array $attr; |
|
23
|
|
|
public array $linkAttr; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(string $label, ?string $icon, ?array $options = []) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->children = []; |
|
28
|
|
|
$this->current = false; |
|
29
|
|
|
$this->inActiveTrail = false; |
|
30
|
|
|
|
|
31
|
|
|
$this->setLabel($label); |
|
32
|
|
|
$this->setIcon($icon); |
|
33
|
|
|
$this->target = $options['target'] ?? '_self'; |
|
34
|
|
|
|
|
35
|
|
|
$this->attr = $options['attr'] ?? []; |
|
36
|
|
|
$this->linkAttr = $options['linkAttr'] ?? []; |
|
37
|
|
|
|
|
38
|
|
|
$this->handle = (new UnicodeString($this->label))->snake()->toString(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getType(): string |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->type; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setType(string $type): static |
|
47
|
|
|
{ |
|
48
|
|
|
$this->type = $type; |
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getLabel(): string |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->label; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function setLabel(string $label): static |
|
58
|
|
|
{ |
|
59
|
|
|
$this->label = $label; |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getIcon(): ?string |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->icon; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function setIcon(?string $icon): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->icon = $icon; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getRouteName(): ?string |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->routeName ?? null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function setRouteName(?string $routeName): static |
|
79
|
|
|
{ |
|
80
|
|
|
$this->routeName = $routeName; |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getRouteParameters(): ?array |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->routeParameters; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function setRouteParameter(string $parameterName, $parameterValue): static |
|
90
|
|
|
{ |
|
91
|
|
|
$this->routeParameters[$parameterName] = $parameterValue; |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function setRouteParameters(?array $routeParameters): static |
|
96
|
|
|
{ |
|
97
|
|
|
$this->routeParameters = $routeParameters; |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getChildren(): array|Traversable |
|
102
|
|
|
{ |
|
103
|
|
|
if (!is_array($this->children)) { |
|
104
|
|
|
$this->children = iterator_to_array($this->children, false); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->children; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function setChildren(callable $children): static |
|
111
|
|
|
{ |
|
112
|
|
|
$this->children = call_user_func($children); |
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function hasChildren(): bool |
|
117
|
|
|
{ |
|
118
|
|
|
if ($this->children instanceof Traversable) { |
|
119
|
|
|
$this->children = iterator_to_array($this->children); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return !empty($this->children); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function isCurrent(): bool |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->current; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param bool $current |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setCurrent(bool $current): void |
|
137
|
|
|
{ |
|
138
|
|
|
$this->current = $current; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
|
|
public function isInActiveTrail(): bool |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->inActiveTrail; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param bool $inActiveTrail |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setInActiveTrail(bool $inActiveTrail): void |
|
153
|
|
|
{ |
|
154
|
|
|
$this->inActiveTrail = $inActiveTrail; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function getUrl(): string |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->url; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function getTarget(): string |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->target ?: '_self'; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function setTarget(string $target): static |
|
168
|
|
|
{ |
|
169
|
|
|
$this->target = $target; |
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function addClass(string $class) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->attr = array_replace([ |
|
176
|
|
|
'class' => implode(' ', [$class, $this->attr['class'] ?? '']) |
|
177
|
|
|
], $this->attr); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
} |