xref: /webtrees/app/Menu.php (revision 09482a558a7989d76059e7f9911605cf836b77ba)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22/**
23 * System for generating menus.
24 */
25class Menu
26{
27    /** @var string The text to be displayed in the menu */
28    private $label;
29
30    /** @var string The target URL or href */
31    private $link;
32
33    /** @var string The CSS class used to style this menu item */
34    private $class;
35
36    /** @var array<string> A list of optional HTML attributes, such as onclick or data-xxx */
37    private $attrs;
38
39    /** @var array<Menu> An optional list of sub-menus. */
40    private $submenus;
41
42    /**
43     * Constructor for the menu class
44     *
45     * @param string        $label    The label for the menu item
46     * @param string        $link     The target URL
47     * @param string        $class    A CSS class
48     * @param array<string> $attrs    Optional attributes, such as onclick or data-xxx
49     * @param array<Menu>   $submenus Any submenus
50     */
51    public function __construct(
52        string $label,
53        string $link = '#',
54        string $class = '',
55        array $attrs = [],
56        array $submenus = []
57    ) {
58        $this
59            ->setLabel($label)
60            ->setLink($link)
61            ->setClass($class)
62            ->setAttrs($attrs)
63            ->setSubmenus($submenus);
64    }
65
66    /**
67     * Get the optional attributes.
68     *
69     * @return array<string>
70     */
71    public function getAttrs(): array
72    {
73        return $this->attrs;
74    }
75
76    /**
77     * Set the optional attributes.
78     *
79     * @param array<string> $attrs
80     *
81     * @return $this
82     */
83    public function setAttrs(array $attrs): self
84    {
85        $this->attrs = $attrs;
86
87        return $this;
88    }
89
90    /**
91     * Get the class.
92     *
93     * @return string
94     */
95    public function getClass(): string
96    {
97        return $this->class;
98    }
99
100    /**
101     * Set the class.
102     *
103     * @param string $class
104     *
105     * @return $this
106     */
107    public function setClass(string $class): self
108    {
109        $this->class = $class;
110
111        return $this;
112    }
113
114    /**
115     * Get the label.
116     *
117     * @return string
118     */
119    public function getLabel(): string
120    {
121        return $this->label;
122    }
123
124    /**
125     * Set the label.
126     *
127     * @param string $label
128     *
129     * @return $this
130     */
131    public function setLabel(string $label): self
132    {
133        $this->label = $label;
134
135        return $this;
136    }
137
138    /**
139     * Get the link.
140     *
141     * @return string
142     */
143    public function getLink(): string
144    {
145        return $this->link;
146    }
147
148    /**
149     * Set the link.
150     *
151     * @param string $link
152     *
153     * @return $this
154     */
155    public function setLink(string $link): self
156    {
157        $this->link = $link;
158
159        return $this;
160    }
161
162    /**
163     * Add a submenu to this menu
164     *
165     * @param Menu $menu
166     *
167     * @return $this
168     */
169    public function addSubmenu(Menu $menu): self
170    {
171        $this->submenus[] = $menu;
172
173        return $this;
174    }
175
176    /**
177     * Get the sub-menus.
178     *
179     * @return array<Menu>
180     */
181    public function getSubmenus(): array
182    {
183        return $this->submenus;
184    }
185
186    /**
187     * Set the sub-menus.
188     *
189     * @param array<Menu> $submenus
190     *
191     * @return $this
192     */
193    public function setSubmenus(array $submenus): self
194    {
195        $this->submenus = $submenus;
196
197        return $this;
198    }
199}
200