xref: /webtrees/app/Menu.php (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22a25f0a04SGreg Roach/**
2376692c8bSGreg Roach * System for generating menus.
24a25f0a04SGreg Roach */
25c1010edaSGreg Roachclass Menu
26c1010edaSGreg Roach{
27*43f2f523SGreg Roach    private string $label;
28a25f0a04SGreg Roach
29*43f2f523SGreg Roach    private string $link;
30a25f0a04SGreg Roach
31*43f2f523SGreg Roach    private string $class;
32a25f0a04SGreg Roach
3309482a55SGreg Roach    /** @var array<string> A list of optional HTML attributes, such as onclick or data-xxx */
34*43f2f523SGreg Roach    private array $attrs;
35a25f0a04SGreg Roach
3609482a55SGreg Roach    /** @var array<Menu> An optional list of sub-menus. */
37*43f2f523SGreg Roach    private array $submenus;
38a25f0a04SGreg Roach
39a25f0a04SGreg Roach    /**
40a25f0a04SGreg Roach     * Constructor for the menu class
41a25f0a04SGreg Roach     *
42a25f0a04SGreg Roach     * @param string        $label    The label for the menu item
43a25f0a04SGreg Roach     * @param string        $link     The target URL
443cf92ae2SGreg Roach     * @param string        $class    A CSS class
4509482a55SGreg Roach     * @param array<string> $attrs    Optional attributes, such as onclick or data-xxx
4609482a55SGreg Roach     * @param array<Menu>   $submenus Any submenus
47a25f0a04SGreg Roach     */
4824f2a3afSGreg Roach    public function __construct(
4924f2a3afSGreg Roach        string $label,
5024f2a3afSGreg Roach        string $link = '#',
5124f2a3afSGreg Roach        string $class = '',
5224f2a3afSGreg Roach        array $attrs = [],
5324f2a3afSGreg Roach        array $submenus = []
5424f2a3afSGreg Roach    ) {
55a25f0a04SGreg Roach        $this
56a25f0a04SGreg Roach            ->setLabel($label)
57a25f0a04SGreg Roach            ->setLink($link)
583d0a6fa1SGreg Roach            ->setClass($class)
593cf92ae2SGreg Roach            ->setAttrs($attrs)
60a25f0a04SGreg Roach            ->setSubmenus($submenus);
61a25f0a04SGreg Roach    }
62a25f0a04SGreg Roach
63a25f0a04SGreg Roach    /**
643cf92ae2SGreg Roach     * Get the optional attributes.
653cf92ae2SGreg Roach     *
6624f2a3afSGreg Roach     * @return array<string>
673cf92ae2SGreg Roach     */
688f53f488SRico Sonntag    public function getAttrs(): array
69c1010edaSGreg Roach    {
703cf92ae2SGreg Roach        return $this->attrs;
713cf92ae2SGreg Roach    }
723cf92ae2SGreg Roach
733cf92ae2SGreg Roach    /**
743cf92ae2SGreg Roach     * Set the optional attributes.
753cf92ae2SGreg Roach     *
7609482a55SGreg Roach     * @param array<string> $attrs
773cf92ae2SGreg Roach     *
783cf92ae2SGreg Roach     * @return $this
793cf92ae2SGreg Roach     */
808f53f488SRico Sonntag    public function setAttrs(array $attrs): self
81c1010edaSGreg Roach    {
823cf92ae2SGreg Roach        $this->attrs = $attrs;
833cf92ae2SGreg Roach
843cf92ae2SGreg Roach        return $this;
853cf92ae2SGreg Roach    }
863cf92ae2SGreg Roach
873cf92ae2SGreg Roach    /**
8876692c8bSGreg Roach     * Get the class.
8976692c8bSGreg Roach     *
90a25f0a04SGreg Roach     * @return string
91a25f0a04SGreg Roach     */
928f53f488SRico Sonntag    public function getClass(): string
93c1010edaSGreg Roach    {
943d0a6fa1SGreg Roach        return $this->class;
95a25f0a04SGreg Roach    }
96a25f0a04SGreg Roach
97a25f0a04SGreg Roach    /**
9876692c8bSGreg Roach     * Set the class.
9976692c8bSGreg Roach     *
1003d0a6fa1SGreg Roach     * @param string $class
101a25f0a04SGreg Roach     *
102a25f0a04SGreg Roach     * @return $this
103a25f0a04SGreg Roach     */
10424f2a3afSGreg Roach    public function setClass(string $class): self
105c1010edaSGreg Roach    {
1063d0a6fa1SGreg Roach        $this->class = $class;
107a25f0a04SGreg Roach
108a25f0a04SGreg Roach        return $this;
109a25f0a04SGreg Roach    }
110a25f0a04SGreg Roach
111a25f0a04SGreg Roach    /**
11276692c8bSGreg Roach     * Get the label.
11376692c8bSGreg Roach     *
114a25f0a04SGreg Roach     * @return string
115a25f0a04SGreg Roach     */
1168f53f488SRico Sonntag    public function getLabel(): string
117c1010edaSGreg Roach    {
118a25f0a04SGreg Roach        return $this->label;
119a25f0a04SGreg Roach    }
120a25f0a04SGreg Roach
121a25f0a04SGreg Roach    /**
12276692c8bSGreg Roach     * Set the label.
12376692c8bSGreg Roach     *
124a25f0a04SGreg Roach     * @param string $label
125a25f0a04SGreg Roach     *
126a25f0a04SGreg Roach     * @return $this
127a25f0a04SGreg Roach     */
12824f2a3afSGreg Roach    public function setLabel(string $label): self
129c1010edaSGreg Roach    {
130a25f0a04SGreg Roach        $this->label = $label;
131a25f0a04SGreg Roach
132a25f0a04SGreg Roach        return $this;
133a25f0a04SGreg Roach    }
134a25f0a04SGreg Roach
135a25f0a04SGreg Roach    /**
13676692c8bSGreg Roach     * Get the link.
1373cf92ae2SGreg Roach     *
138a25f0a04SGreg Roach     * @return string
139a25f0a04SGreg Roach     */
1408f53f488SRico Sonntag    public function getLink(): string
141c1010edaSGreg Roach    {
142a25f0a04SGreg Roach        return $this->link;
143a25f0a04SGreg Roach    }
144a25f0a04SGreg Roach
145a25f0a04SGreg Roach    /**
14676692c8bSGreg Roach     * Set the link.
14776692c8bSGreg Roach     *
148a25f0a04SGreg Roach     * @param string $link
149a25f0a04SGreg Roach     *
150a25f0a04SGreg Roach     * @return $this
151a25f0a04SGreg Roach     */
15224f2a3afSGreg Roach    public function setLink(string $link): self
153c1010edaSGreg Roach    {
154a25f0a04SGreg Roach        $this->link = $link;
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach        return $this;
157a25f0a04SGreg Roach    }
158a25f0a04SGreg Roach
159a25f0a04SGreg Roach    /**
160a25f0a04SGreg Roach     * Add a submenu to this menu
161a25f0a04SGreg Roach     *
162ecb5b5e9SGreg Roach     * @param Menu $menu
163ecb5b5e9SGreg Roach     *
164ecb5b5e9SGreg Roach     * @return $this
165a25f0a04SGreg Roach     */
16624f2a3afSGreg Roach    public function addSubmenu(Menu $menu): self
167c1010edaSGreg Roach    {
168a25f0a04SGreg Roach        $this->submenus[] = $menu;
169ecb5b5e9SGreg Roach
170ecb5b5e9SGreg Roach        return $this;
171a25f0a04SGreg Roach    }
172a25f0a04SGreg Roach
173a25f0a04SGreg Roach    /**
17476692c8bSGreg Roach     * Get the sub-menus.
17576692c8bSGreg Roach     *
17609482a55SGreg Roach     * @return array<Menu>
177a25f0a04SGreg Roach     */
1788f53f488SRico Sonntag    public function getSubmenus(): array
179c1010edaSGreg Roach    {
180a25f0a04SGreg Roach        return $this->submenus;
181a25f0a04SGreg Roach    }
182a25f0a04SGreg Roach
183a25f0a04SGreg Roach    /**
18476692c8bSGreg Roach     * Set the sub-menus.
18576692c8bSGreg Roach     *
18609482a55SGreg Roach     * @param array<Menu> $submenus
187a25f0a04SGreg Roach     *
188a25f0a04SGreg Roach     * @return $this
189a25f0a04SGreg Roach     */
1908f53f488SRico Sonntag    public function setSubmenus(array $submenus): self
191c1010edaSGreg Roach    {
192a25f0a04SGreg Roach        $this->submenus = $submenus;
193a25f0a04SGreg Roach
194a25f0a04SGreg Roach        return $this;
195a25f0a04SGreg Roach    }
196a25f0a04SGreg Roach}
197