Completed
Push — master ( d1e0a9...cb6f48 )
by David
11s
created

MenuItem::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
     * Whether the menu is extended or not.
39
     * This should not have an effect if the menu has no child.
40
     *
41
     * @var bool
42
     */
43
    private $isExtended;
0 ignored issues
show
introduced by
The private property $isExtended is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @param string $label The text for the menu item
47
     * @param string|null $url The link for the menu (relative to the root url), unless it starts with / or http:// or https:// or # or ?.
48
     */
49
    public function __construct(string $label, string $url=null) {
50
        $this->label = $label;
51
        $this->url = $url;
52
        $this->children = new \SplPriorityQueue();
53
    }
54
55
    /**
56
     * Returns the label for the menu item.
57
     * @return string
58
     */
59
    public function getLabel(): string {
60
        return $this->label;
61
    }
62
63
    /**
64
     * Returns the URL for this menu (or null if this menu is not a link).
65
     * @return string|null
66
     */
67
    public function getUrl(): ?string {
68
        return $this->url;
69
    }
70
71
    public function findChild(string $label): ?MenuItem
72
    {
73
        foreach ($this->children as $child) {
74
            if ($child->getLabel() === $label) {
75
                return $child;
76
            }
77
        }
78
        return null;
79
    }
80
81
    /**
82
     * Returns a list of children elements for the menu (if there are some).
83
     *
84
     * Note: a SplPriorityQueue can be iterated only once so we clone the whole queue and turn it into an array
85
     *
86
     * @return MenuItem[]
87
     */
88
    public function getChildren(): array {
89
        return iterator_to_array(clone $this->children);
90
    }
91
92
    /**
93
     * Adds a menu item as a child of this menu item.
94
     *
95
     * @param MenuItem $menuItem
96
     */
97
    public function addMenuItem(MenuItem $menuItem, float $priority): void {
98
        $this->children->insert($menuItem, $priority);
99
    }
100
101
102
    public function isActive(string $url): bool
103
    {
104
        return $url === $this->url;
105
    }
106
107
    /**
108
     * Returns true if the menu should be in extended state (if one of the children is in active state).
109
     * @return bool
110
     */
111
    public function isExtended(string $url): bool {
112
        foreach ($this->children as $child) {
113
            if ($child->isActive($url) || $child->isExtended($url)) {
114
                return true;
115
            }
116
        }
117
118
        return false;
119
    }
120
121
    /**
122
     * Returns an optional CSS class to apply to the menu item.
123
     * @return string|null
124
     */
125
    public function getCssClass(): ?string {
126
        return $this->cssClass;
127
    }
128
129
    /**
130
     * An optional CSS class to apply to the menu item.
131
     * Use of this property depends on the menu implementation.
132
     *
133
     * @param string|null $cssClass
134
     */
135
    public function setCssClass(?string $cssClass): void {
136
        $this->cssClass = $cssClass;
137
    }
138
}
139