xref: /webtrees/app/Menu.php (revision 73b8c04073f4aae42af235f4485847d0a07c8f8c)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use function trigger_error;
23
24use const E_USER_DEPRECATED;
25
26/**
27 * System for generating menus.
28 */
29class Menu
30{
31    /** @var string The text to be displayed in the menu */
32    private $label;
33
34    /** @var string The target URL or href */
35    private $link;
36
37    /** @var string The CSS class used to style this menu item */
38    private $class;
39
40    /** @var string[] A list of optional HTML attributes, such as onclick or data-xxx */
41    private $attrs;
42
43    /** @var Menu[] An optional list of sub-menus. */
44    private $submenus;
45
46    /**
47     * Constructor for the menu class
48     *
49     * @param string   $label    The label for the menu item
50     * @param string   $link     The target URL
51     * @param string   $class    A CSS class
52     * @param string[] $attrs    Optional attributes, such as onclick or data-xxx
53     * @param Menu[]   $submenus Any submenus
54     */
55    public function __construct($label, $link = '#', $class = '', array $attrs = [], array $submenus = [])
56    {
57        $this
58            ->setLabel($label)
59            ->setLink($link)
60            ->setClass($class)
61            ->setAttrs($attrs)
62            ->setSubmenus($submenus);
63    }
64
65    /**
66     * Render this menu using Bootstrap4 markup
67     *
68     * @return string
69     *
70     * @deprecated since 2.0.2.  Will be removed in 2.1.0
71     */
72    public function bootstrap4(): string
73    {
74        trigger_error(
75            'Menu::bootstrap4() is deprecated.  Use the view(components/menu-item) instead',
76            E_USER_DEPRECATED
77        );
78
79        return view('components/menu-item', ['menu' => $this]);
80    }
81
82    /**
83     * Get the optional attributes.
84     *
85     * @return string[]
86     */
87    public function getAttrs(): array
88    {
89        return $this->attrs;
90    }
91
92    /**
93     * Set the optional attributes.
94     *
95     * @param string[] $attrs
96     *
97     * @return $this
98     */
99    public function setAttrs(array $attrs): self
100    {
101        $this->attrs = $attrs;
102
103        return $this;
104    }
105
106    /**
107     * Get the class.
108     *
109     * @return string
110     */
111    public function getClass(): string
112    {
113        return $this->class;
114    }
115
116    /**
117     * Set the class.
118     *
119     * @param string $class
120     *
121     * @return $this
122     */
123    public function setClass($class): self
124    {
125        $this->class = $class;
126
127        return $this;
128    }
129
130    /**
131     * Get the label.
132     *
133     * @return string
134     */
135    public function getLabel(): string
136    {
137        return $this->label;
138    }
139
140    /**
141     * Set the label.
142     *
143     * @param string $label
144     *
145     * @return $this
146     */
147    public function setLabel($label): self
148    {
149        $this->label = $label;
150
151        return $this;
152    }
153
154    /**
155     * Get the link.
156     *
157     * @return string
158     */
159    public function getLink(): string
160    {
161        return $this->link;
162    }
163
164    /**
165     * Set the link.
166     *
167     * @param string $link
168     *
169     * @return $this
170     */
171    public function setLink($link): self
172    {
173        $this->link = $link;
174
175        return $this;
176    }
177
178    /**
179     * Add a submenu to this menu
180     *
181     * @param Menu $menu
182     *
183     * @return $this
184     */
185    public function addSubmenu($menu): self
186    {
187        $this->submenus[] = $menu;
188
189        return $this;
190    }
191
192    /**
193     * Get the sub-menus.
194     *
195     * @return Menu[]
196     */
197    public function getSubmenus(): array
198    {
199        return $this->submenus;
200    }
201
202    /**
203     * Set the sub-menus.
204     *
205     * @param Menu[] $submenus
206     *
207     * @return $this
208     */
209    public function setSubmenus(array $submenus): self
210    {
211        $this->submenus = $submenus;
212
213        return $this;
214    }
215}
216