Completed
Pull Request — master (#1)
by David
01:36
created

MenuRegistry::getRootMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 branch 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 string[] $items The path from the root menu to the branch we are interested in.
40
     * @param null|string $url
41
     * @param float $priority
42
     * @param string|null $cssClass
43
     */
44
    public function registerSubMenuItem(MenuItem $menuItem, array $items, ?string $url, float $priority = 0.0, ?string $cssClass): void
45
    {
46
        $label = array_shift($items);
47
48
        if (\count($items) > 0) {
49
            $childMenuItem = $menuItem->findChild($label);
50
            if ($childMenuItem === null) {
51
                $childMenuItem = new MenuItem($label);
52
                $menuItem->addMenuItem($childMenuItem, 0);
53
            }
54
            $this->registerSubMenuItem($childMenuItem, $items, $url, $priority, $cssClass);
55
            return;
56
        }
57
58
        $childMenuItem = new MenuItem($label, $url);
59
        $childMenuItem->setCssClass($cssClass);
60
        $menuItem->addMenuItem($childMenuItem, $priority);
61
    }
62
}
63