1 | <?php |
||
7 | class Builder |
||
8 | { |
||
9 | protected const ADD_AFTER = 0; |
||
10 | protected const ADD_BEFORE = 1; |
||
11 | protected const ADD_INSIDE = 2; |
||
12 | |||
13 | /** |
||
14 | * The set of menu items. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | public $menu = []; |
||
19 | |||
20 | /** |
||
21 | * The set of filters applied to menu items. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private $filters; |
||
26 | |||
27 | /** |
||
28 | * Constructor. |
||
29 | * |
||
30 | * @param array $filters |
||
31 | */ |
||
32 | 49 | public function __construct(array $filters = []) |
|
36 | |||
37 | /** |
||
38 | * Add new items at the end of the menu. |
||
39 | * |
||
40 | * @param mixed $newItems Items to be added |
||
41 | */ |
||
42 | 49 | public function add(...$newItems) |
|
47 | |||
48 | /** |
||
49 | * Add new items after a specific menu item. |
||
50 | * |
||
51 | * @param mixed $itemKey The key that represents the specific menu item |
||
52 | * @param mixed $newItems Items to be added |
||
53 | */ |
||
54 | 5 | public function addAfter($itemKey, ...$newItems) |
|
58 | |||
59 | /** |
||
60 | * Add new items before a specific menu item. |
||
61 | * |
||
62 | * @param mixed $itemKey The key that represents the specific menu item |
||
63 | * @param mixed $newItems Items to be added |
||
64 | */ |
||
65 | 5 | public function addBefore($itemKey, ...$newItems) |
|
69 | |||
70 | /** |
||
71 | * Add new submenu items inside a specific menu item. |
||
72 | * |
||
73 | * @param mixed $itemKey The key that represents the specific menu item |
||
74 | * @param mixed $newItems Items to be added |
||
75 | */ |
||
76 | 4 | public function addIn($itemKey, ...$newItems) |
|
80 | |||
81 | /** |
||
82 | * Remove a specific menu item. |
||
83 | * |
||
84 | * @param mixed $itemKey The key of the menu item to remove |
||
85 | */ |
||
86 | 5 | public function remove($itemKey) |
|
104 | |||
105 | /** |
||
106 | * Check if exists a menu item with the specified key. |
||
107 | * |
||
108 | * @param mixed $itemKey The key of the menu item to check for |
||
109 | * @return bool |
||
110 | */ |
||
111 | 3 | public function itemKeyExists($itemKey) |
|
115 | |||
116 | /** |
||
117 | * Transform the items by applying the filters. |
||
118 | * |
||
119 | * @param array $items An array with items to be transformed |
||
120 | * @return array Array with the new transformed items |
||
121 | */ |
||
122 | 49 | public function transformItems($items) |
|
126 | |||
127 | /** |
||
128 | * Find a menu item by the item key and return the path to it. |
||
129 | * |
||
130 | * @param mixed $itemKey The key of the item to find |
||
131 | * @param array $items The array to look up for the item |
||
132 | * @return mixed Array with the path sequence, or empty array if not found |
||
133 | */ |
||
134 | 22 | protected function findItem($itemKey, $items) |
|
156 | |||
157 | /** |
||
158 | * Apply all the available filters to a menu item. |
||
159 | * |
||
160 | * @param mixed $item A menu item |
||
161 | * @return mixed A new item with all the filters applied |
||
162 | */ |
||
163 | 49 | protected function applyFilters($item) |
|
164 | { |
||
165 | 49 | if (is_string($item)) { |
|
166 | return $item; |
||
167 | } |
||
168 | |||
169 | 49 | foreach ($this->filters as $filter) { |
|
170 | 44 | $item = $filter->transform($item, $this); |
|
171 | } |
||
172 | |||
173 | 49 | return $item; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Add new items to the menu in a particular place, relative to a |
||
178 | * specific menu item. |
||
179 | * |
||
180 | * @param mixed $itemKey The key that represents the specific menu item |
||
181 | * @param int $where Where to add the new items |
||
182 | * @param mixed $newItems Items to be added |
||
183 | */ |
||
184 | 14 | protected function addItem($itemKey, $where, ...$newItems) |
|
214 | } |
||
215 |