xref: /webtrees/app/Menu.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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    private string $label;
28
29    private string $link;
30
31    private string $class;
32
33    /** @var array<string> A list of optional HTML attributes, such as onclick or data-xxx */
34    private array $attrs;
35
36    /** @var array<Menu> An optional list of sub-menus. */
37    private array $submenus;
38
39    /**
40     * Constructor for the menu class
41     *
42     * @param string        $label    The label for the menu item
43     * @param string        $link     The target URL
44     * @param string        $class    A CSS class
45     * @param array<string> $attrs    Optional attributes, such as onclick or data-xxx
46     * @param array<Menu>   $submenus Any submenus
47     */
48    public function __construct(
49        string $label,
50        string $link = '#',
51        string $class = '',
52        array $attrs = [],
53        array $submenus = []
54    ) {
55        $this
56            ->setLabel($label)
57            ->setLink($link)
58            ->setClass($class)
59            ->setAttrs($attrs)
60            ->setSubmenus($submenus);
61    }
62
63    /**
64     * Get the optional attributes.
65     *
66     * @return array<string>
67     */
68    public function getAttrs(): array
69    {
70        return $this->attrs;
71    }
72
73    /**
74     * Set the optional attributes.
75     *
76     * @param array<string> $attrs
77     *
78     * @return self
79     */
80    public function setAttrs(array $attrs): self
81    {
82        $this->attrs = $attrs;
83
84        return $this;
85    }
86
87    /**
88     * Get the class.
89     *
90     * @return string
91     */
92    public function getClass(): string
93    {
94        return $this->class;
95    }
96
97    /**
98     * Set the class.
99     *
100     * @param string $class
101     *
102     * @return self
103     */
104    public function setClass(string $class): self
105    {
106        $this->class = $class;
107
108        return $this;
109    }
110
111    /**
112     * Get the label.
113     *
114     * @return string
115     */
116    public function getLabel(): string
117    {
118        return $this->label;
119    }
120
121    /**
122     * Set the label.
123     *
124     * @param string $label
125     *
126     * @return self
127     */
128    public function setLabel(string $label): self
129    {
130        $this->label = $label;
131
132        return $this;
133    }
134
135    /**
136     * Get the link.
137     *
138     * @return string
139     */
140    public function getLink(): string
141    {
142        return $this->link;
143    }
144
145    /**
146     * Set the link.
147     *
148     * @param string $link
149     *
150     * @return self
151     */
152    public function setLink(string $link): self
153    {
154        $this->link = $link;
155
156        return $this;
157    }
158
159    /**
160     * Add a submenu to this menu
161     *
162     * @param Menu $menu
163     *
164     * @return self
165     */
166    public function addSubmenu(Menu $menu): self
167    {
168        $this->submenus[] = $menu;
169
170        return $this;
171    }
172
173    /**
174     * Get the sub-menus.
175     *
176     * @return array<Menu>
177     */
178    public function getSubmenus(): array
179    {
180        return $this->submenus;
181    }
182
183    /**
184     * Set the sub-menus.
185     *
186     * @param array<Menu> $submenus
187     *
188     * @return self
189     */
190    public function setSubmenus(array $submenus): self
191    {
192        $this->submenus = $submenus;
193
194        return $this;
195    }
196}
197