cff0b1fa60ca5e3f2a446aa350a33d9d52e88f89
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Template / Components / Menu / Menu.php
1 <?php
2 namespace TYPO3\CMS\Backend\Template\Components\Menu;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Menu
21 */
22 class Menu
23 {
24 /**
25 * Menu Identifier
26 *
27 * @var string
28 */
29 protected $identifier = '';
30
31 /**
32 * Label of the Menu (useful for Selectbox menus)
33 *
34 * @var string
35 */
36 protected $label = '';
37
38 /**
39 * Container for menuitems
40 *
41 * @var array
42 */
43 protected $menuItems = [];
44
45 /**
46 * Get the label
47 *
48 * @return string
49 */
50 public function getLabel()
51 {
52 return $this->label;
53 }
54
55 /**
56 * Set label
57 *
58 * @param string $label LabelText for the menu (accepts LLL syntax)
59 *
60 * @return Menu
61 */
62 public function setLabel($label)
63 {
64 $this->label = $label;
65 return $this;
66 }
67
68 /**
69 * Set identifier
70 *
71 * @param string $identifier Menu Identifier
72 *
73 * @return Menu
74 */
75 public function setIdentifier($identifier)
76 {
77 $this->identifier = $identifier;
78 return $this;
79 }
80
81 /**
82 * Adds a new menuItem
83 *
84 * @param MenuItem $menuItem The menuItem to add to the menu
85 *
86 * @throws \InvalidArgumentException In case a menuItem is not valid
87 *
88 * @return void
89 */
90 public function addMenuItem(MenuItem $menuItem)
91 {
92 if (!$menuItem->isValid($menuItem)) {
93 throw new \InvalidArgumentException('MenuItem "' . $menuItem->getTitle() . '" is not valid', 1442236317);
94 }
95 // @todo implement sorting of menu items
96 // @todo maybe even things like spacers/sections?
97 $this->menuItems[] = clone $menuItem;
98 }
99
100 /**
101 * Get menu items
102 *
103 * @return array
104 */
105 public function getMenuItems()
106 {
107 return $this->menuItems;
108 }
109
110 /**
111 * Get identifier
112 *
113 * @return string
114 */
115 public function getIdentifier()
116 {
117 return $this->identifier;
118 }
119
120 /**
121 * MenuItem Factory Method
122 *
123 * @return MenuItem
124 */
125 public function makeMenuItem()
126 {
127 $menuItem = GeneralUtility::makeInstance(MenuItem::class);
128 return $menuItem;
129 }
130
131 /**
132 * Validation function
133 *
134 * @param Menu $menu The menu to validate
135 *
136 * @return bool
137 */
138 public function isValid(Menu $menu)
139 {
140 return (trim($menu->getIdentifier()) !== '');
141 }
142 }