1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\CMS\StaticRegistry\Menu; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class MenuRegistry |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var MenuItem |
11
|
|
|
*/ |
12
|
|
|
private $rootMenu; |
13
|
|
|
|
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
$this->rootMenu = new MenuItem('root'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return MenuItem |
21
|
|
|
*/ |
22
|
|
|
public function getRootMenu(): MenuItem |
23
|
|
|
{ |
24
|
|
|
return $this->rootMenu; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string[] $items The path from the root menu to the leaf we are interested in. |
29
|
|
|
* @param null|string $url |
30
|
|
|
* @param float $priority |
31
|
|
|
* @param string|null $cssClass |
32
|
|
|
*/ |
33
|
|
|
public function registerMenuItem(array $items, ?string $url, float $priority = 0.0, ?string $cssClass = null): void |
34
|
|
|
{ |
35
|
|
|
$this->registerSubMenuItem($this->rootMenu, $items, $url, $priority, $cssClass); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param MenuItem $menuItem |
40
|
|
|
* @param string[] $items The path from $menuItem to the leaf we are interested in. |
41
|
|
|
* @param null|string $url |
42
|
|
|
* @param float $priority |
43
|
|
|
* @param string|null $cssClass |
44
|
|
|
*/ |
45
|
|
|
private function registerSubMenuItem(MenuItem $menuItem, array $items, ?string $url, float $priority = 0.0, ?string $cssClass): void |
46
|
|
|
{ |
47
|
|
|
$label = array_shift($items); |
48
|
|
|
|
49
|
|
|
if (\count($items) > 0) { |
50
|
|
|
$childMenuItem = $menuItem->findChild($label); |
51
|
|
|
if ($childMenuItem === null) { |
52
|
|
|
$childMenuItem = new MenuItem($label); |
53
|
|
|
$menuItem->addMenuItem($childMenuItem, 0); |
54
|
|
|
} |
55
|
|
|
$this->registerSubMenuItem($childMenuItem, $items, $url, $priority, $cssClass); |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$childMenuItem = new MenuItem($label, $url); |
60
|
|
|
$childMenuItem->setCssClass($cssClass); |
61
|
|
|
$menuItem->addMenuItem($childMenuItem, $priority); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|