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