xref: /webtrees/app/Menu.php (revision 0d047a8c74753c7558a60f4789c838996d6fae8b)
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
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(
56        string $label,
57        string $link = '#',
58        string $class = '',
59        array $attrs = [],
60        array $submenus = []
61    ) {
62        $this
63            ->setLabel($label)
64            ->setLink($link)
65            ->setClass($class)
66            ->setAttrs($attrs)
67            ->setSubmenus($submenus);
68    }
69
70    /**
71     * Render this menu using Bootstrap4 markup
72     *
73     * @return string
74     *
75     * @deprecated since 2.0.2.  Will be removed in 2.1.0
76     */
77    public function bootstrap4(): string
78    {
79        trigger_error(
80            'Menu::bootstrap4() is deprecated.  Use the view(components/menu-item) instead',
81            E_USER_DEPRECATED
82        );
83
84        return view('components/menu-item', ['menu' => $this]);
85    }
86
87    /**
88     * Get the optional attributes.
89     *
90     * @return array<string>
91     */
92    public function getAttrs(): array
93    {
94        return $this->attrs;
95    }
96
97    /**
98     * Set the optional attributes.
99     *
100     * @param string[] $attrs
101     *
102     * @return $this
103     */
104    public function setAttrs(array $attrs): self
105    {
106        $this->attrs = $attrs;
107
108        return $this;
109    }
110
111    /**
112     * Get the class.
113     *
114     * @return string
115     */
116    public function getClass(): string
117    {
118        return $this->class;
119    }
120
121    /**
122     * Set the class.
123     *
124     * @param string $class
125     *
126     * @return $this
127     */
128    public function setClass(string $class): self
129    {
130        $this->class = $class;
131
132        return $this;
133    }
134
135    /**
136     * Get the label.
137     *
138     * @return string
139     */
140    public function getLabel(): string
141    {
142        return $this->label;
143    }
144
145    /**
146     * Set the label.
147     *
148     * @param string $label
149     *
150     * @return $this
151     */
152    public function setLabel(string $label): self
153    {
154        $this->label = $label;
155
156        return $this;
157    }
158
159    /**
160     * Get the link.
161     *
162     * @return string
163     */
164    public function getLink(): string
165    {
166        return $this->link;
167    }
168
169    /**
170     * Set the link.
171     *
172     * @param string $link
173     *
174     * @return $this
175     */
176    public function setLink(string $link): self
177    {
178        $this->link = $link;
179
180        return $this;
181    }
182
183    /**
184     * Add a submenu to this menu
185     *
186     * @param Menu $menu
187     *
188     * @return $this
189     */
190    public function addSubmenu(Menu $menu): self
191    {
192        $this->submenus[] = $menu;
193
194        return $this;
195    }
196
197    /**
198     * Get the sub-menus.
199     *
200     * @return Menu[]
201     */
202    public function getSubmenus(): array
203    {
204        return $this->submenus;
205    }
206
207    /**
208     * Set the sub-menus.
209     *
210     * @param Menu[] $submenus
211     *
212     * @return $this
213     */
214    public function setSubmenus(array $submenus): self
215    {
216        $this->submenus = $submenus;
217
218        return $this;
219    }
220}
221