|
1
|
|
|
<?php |
|
2
|
|
|
namespace TheCodingMachine\CMS\StaticRegistry\Menu; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* This class represent a menu item. |
|
6
|
|
|
*/ |
|
7
|
|
|
class MenuItem { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The text for the menu item |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $label; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The link for the menu (relative to the root url), unless it starts with / or http:// or https:// or # or ?. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string|null |
|
20
|
|
|
*/ |
|
21
|
|
|
private $url; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The children menu item of this menu (if any). |
|
25
|
|
|
* |
|
26
|
|
|
* @var \SplPriorityQueue|MenuItem[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $children; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The CSS class for the menu, if any. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $cssClass; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $label The text for the menu item |
|
39
|
|
|
* @param string|null $url The link for the menu (relative to the root url), unless it starts with / or http:// or https:// or # or ?. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(string $label, string $url=null) { |
|
42
|
|
|
$this->label = $label; |
|
43
|
|
|
$this->url = $url; |
|
44
|
|
|
$this->children = new \SplPriorityQueue(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns the label for the menu item. |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getLabel(): string { |
|
52
|
|
|
return $this->label; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the URL for this menu (or null if this menu is not a link). |
|
57
|
|
|
* @return string|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getUrl(): ?string { |
|
60
|
|
|
return $this->url; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function findChild(string $label): ?MenuItem |
|
64
|
|
|
{ |
|
65
|
|
|
foreach (clone $this->children as $child) { |
|
66
|
|
|
if ($child->getLabel() === $label) { |
|
67
|
|
|
return $child; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns a list of children elements for the menu (if there are some). |
|
75
|
|
|
* |
|
76
|
|
|
* Note: a SplPriorityQueue can be iterated only once so we clone the whole queue and turn it into an array |
|
77
|
|
|
* |
|
78
|
|
|
* @return MenuItem[] |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getChildren(): array { |
|
81
|
|
|
return iterator_to_array(clone $this->children); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Adds a menu item as a child of this menu item. |
|
86
|
|
|
* |
|
87
|
|
|
* @param MenuItem $menuItem |
|
88
|
|
|
*/ |
|
89
|
|
|
public function addMenuItem(MenuItem $menuItem, float $priority): void { |
|
90
|
|
|
$this->children->insert($menuItem, $priority); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
public function isActive(string $url): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return $url === $this->url; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns true if the menu should be in extended state (if one of the children is in active state). |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function isExtended(string $url): bool { |
|
104
|
|
|
foreach (clone $this->children as $child) { |
|
105
|
|
|
if ($child->isActive($url) || $child->isExtended($url)) { |
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Returns an optional CSS class to apply to the menu item. |
|
115
|
|
|
* @return string|null |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getCssClass(): ?string { |
|
118
|
|
|
return $this->cssClass; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* An optional CSS class to apply to the menu item. |
|
123
|
|
|
* Use of this property depends on the menu implementation. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string|null $cssClass |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setCssClass(?string $cssClass): void { |
|
128
|
|
|
$this->cssClass = $cssClass; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|