PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / menu / abstractlist.php
vikappointments / site / helpers / libraries / menu Last commit date
horizontal 3 days ago leftboard 3 days ago abstractlist.php 3 days ago custom.php 3 days ago factory.php 3 days ago index.html 3 days ago item.php 3 days ago separator.php 3 days ago shape.php 3 days ago
abstractlist.php
243 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Abstract list menu class that implements common methods.
16 *
17 * @since 1.6.3
18 */
19 class MenuAbstractList
20 {
21 /**
22 * A list of elements.
23 *
24 * @var array
25 */
26 protected $menu;
27
28 /**
29 * The construct sets all the items contained in the menu.
30 *
31 * @param array $menu The menu to push.
32 *
33 * @uses setMenu()
34 */
35 public function __construct(array $menu = array())
36 {
37 $this->setMenu($menu);
38 }
39
40 /**
41 * Pushes an item into the menu.
42 *
43 * @param mixed $item The item to push.
44 *
45 * @return self This object to support chaining.
46 */
47 public function push($item)
48 {
49 $this->menu[] = $item;
50
51 return $this;
52 }
53
54 /**
55 * Returns the index of the requested item.
56 * It is possible to obtain an item by reference or through
57 * an associative array that determines the query arguments.
58 *
59 * @param mixed $item The item reference or a search query.
60 *
61 * @return mixed The item index, false otherwise.
62 */
63 public function indexOf($item)
64 {
65 // iterate items
66 foreach ($this->menu as $index => $tmp)
67 {
68 // look for the item reference
69 if (is_object($item))
70 {
71 // check if the reference is the same
72 if ($tmp === $item)
73 {
74 // return item index
75 return $index;
76 }
77 }
78 // look for a query
79 else if (is_array($item))
80 {
81 $found = true;
82
83 // iterate query
84 foreach ($item as $k => $v)
85 {
86 $match = null;
87
88 // access item value
89 if (isset($tmp->{$k}))
90 {
91 // use direct property
92 $match = $tmp->{$k};
93 }
94 else
95 {
96 // fallback to getter method
97 $getter = 'get' . ucfirst($k);
98
99 if (method_exists($tmp, $getter))
100 {
101 $match = $tmp->{$getter}();
102 }
103 }
104
105 // make sure there is something to match and the return value matches the specified one
106 $found = $found && ($match && $tmp->{$getter}() == $v);
107 }
108
109 if ($found === true)
110 {
111 // item found, return index
112 return $index;
113 }
114 }
115 }
116
117 return false;
118 }
119
120 /**
121 * Returns the reference of the requested item.
122 * It is possible to obtain an item by index or through
123 * an associative array that determines the query arguments.
124 *
125 * @param mixed $item The item index or a search query.
126 *
127 * @return mixed The item found, null otherwise.
128 *
129 * @uses indexOf()
130 */
131 public function get($item)
132 {
133 // look for an item at index (if numeric)
134 if (is_numeric($item))
135 {
136 if (isset($this->menu[$item]))
137 {
138 return $this->menu[$item];
139 }
140 }
141 else
142 {
143 // get item index
144 $index = $this->indexOf($item);
145
146 if ($index !== false)
147 {
148 // item found, return it
149 return $this->menu[$index];
150 }
151 }
152
153 return null;
154 }
155
156 /**
157 * Changes the item at the specified position.
158 *
159 * @param mixed $item The item index, reference or a search query.
160 * @param mixed $set The item to set.
161 *
162 * @return boolean True if changed, false otherwise.
163 *
164 * @uses indexOf()
165 */
166 public function set($item, $set)
167 {
168 if (!is_numeric($item))
169 {
170 // access item index
171 $item = $this->indexOf($item);
172 }
173
174 // make sure the menu exists
175 if ($item === false || !isset($this->menu[$item]))
176 {
177 // item not found
178 return false;
179 }
180
181 // replace item at position found
182 $this->menu[$item] = $set;
183
184 return true;
185 }
186
187 /**
188 * Deletes the requested item.
189 * It is possible to delete an item by index, reference or through
190 * an associative array that determines the query arguments.
191 *
192 * @param mixed $item The item index, reference or a search query.
193 *
194 * @return boolean True if removed, false otherwise.
195 *
196 * @uses indexOf()
197 *
198 * @since 1.6.3
199 */
200 public function delete($item)
201 {
202 if (!is_numeric($item))
203 {
204 // access item index
205 $item = $this->indexOf($item);
206 }
207
208 // make sure the menu exists
209 if ($item === false || !isset($this->menu[$item]))
210 {
211 // item not found
212 return false;
213 }
214
215 // splice menu and remove item found
216 return (bool) array_splice($this->menu, $item, 1);
217 }
218
219 /**
220 * Sets the items in the menu.
221 *
222 * @param array $menu The array containing the items to push.
223 *
224 * @return self This object to support chaining.
225 */
226 public function setMenu(array $menu = array())
227 {
228 $this->menu = $menu;
229
230 return $this;
231 }
232
233 /**
234 * Returns the list of the items in the menu.
235 *
236 * @return array The list of the items.
237 */
238 public function getMenu()
239 {
240 return $this->menu;
241 }
242 }
243