1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Utilities
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\String\StringHelper;
13 use Joomla\Utilities\ArrayHelper;
14
15 /**
16 * JArrayHelper is an array utility class for doing all sorts of odds and ends with arrays.
17 *
18 * @since 11.1
19 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper instead
20 */
21 abstract class JArrayHelper
22 {
23 /**
24 * Option to perform case-sensitive sorts.
25 *
26 * @var mixed Boolean or array of booleans.
27 * @since 11.3
28 */
29 protected static $sortCase;
30
31 /**
32 * Option to set the sort direction.
33 *
34 * @var mixed Integer or array of integers.
35 * @since 11.3
36 */
37 protected static $sortDirection;
38
39 /**
40 * Option to set the object key to sort on.
41 *
42 * @var string
43 * @since 11.3
44 */
45 protected static $sortKey;
46
47 /**
48 * Option to perform a language aware sort.
49 *
50 * @var mixed Boolean or array of booleans.
51 * @since 11.3
52 */
53 protected static $sortLocale;
54
55 /**
56 * Function to convert array to integer values
57 *
58 * @param array &$array The source array to convert
59 * @param mixed $default A default value (int|array) to assign if $array is not an array
60 *
61 * @return void
62 *
63 * @since 11.1
64 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::toInteger instead
65 */
66 public static function toInteger(&$array, $default = null)
67 {
68 $array = ArrayHelper::toInteger($array, $default);
69 }
70
71 /**
72 * Utility function to map an array to a stdClass object.
73 *
74 * @param array &$array The array to map.
75 * @param string $class Name of the class to create
76 * @param boolean $recursive Convert also any array inside the main array
77 *
78 * @return object The object mapped from the given array
79 *
80 * @since 11.1
81 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::toObject instead
82 */
83 public static function toObject(&$array, $class = 'stdClass', $recursive = true)
84 {
85 $obj = null;
86
87 if (is_array($array))
88 {
89 $obj = ArrayHelper::toObject($array, $class, $recursive);
90 }
91 else
92 {
93 JLog::add('This method is typehinted to be an array in \Joomla\Utilities\ArrayHelper::toObject.', JLog::WARNING, 'deprecated');
94 }
95
96 return $obj;
97 }
98
99 /**
100 * Utility function to map an array to a string.
101 *
102 * @param array $array The array to map.
103 * @param string $inner_glue The glue (optional, defaults to '=') between the key and the value.
104 * @param string $outer_glue The glue (optional, defaults to ' ') between array elements.
105 * @param boolean $keepOuterKey True if final key should be kept.
106 *
107 * @return string The string mapped from the given array
108 *
109 * @since 11.1
110 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::toString instead
111 */
112 public static function toString($array = null, $inner_glue = '=', $outer_glue = ' ', $keepOuterKey = false)
113 {
114 $output = array();
115
116 if (is_array($array))
117 {
118 $output[] = ArrayHelper::toString($array, $inner_glue, $outer_glue, $keepOuterKey);
119 }
120 else
121 {
122 JLog::add('This method is typehinted to be an array in \Joomla\Utilities\ArrayHelper::toString.', JLog::WARNING, 'deprecated');
123 }
124
125 return implode($outer_glue, $output);
126 }
127
128 /**
129 * Utility function to map an object to an array
130 *
131 * @param object $p_obj The source object
132 * @param boolean $recurse True to recurse through multi-level objects
133 * @param string $regex An optional regular expression to match on field names
134 *
135 * @return array The array mapped from the given object
136 *
137 * @since 11.1
138 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::fromObject instead
139 */
140 public static function fromObject($p_obj, $recurse = true, $regex = null)
141 {
142 if (is_object($p_obj))
143 {
144 return self::_fromObject($p_obj, $recurse, $regex);
145 }
146 else
147 {
148 return null;
149 }
150 }
151
152 /**
153 * Utility function to map an object or array to an array
154 *
155 * @param mixed $item The source object or array
156 * @param boolean $recurse True to recurse through multi-level objects
157 * @param string $regex An optional regular expression to match on field names
158 *
159 * @return array The array mapped from the given object
160 *
161 * @since 11.1
162 */
163 protected static function _fromObject($item, $recurse, $regex)
164 {
165 if (is_object($item))
166 {
167 $result = array();
168
169 foreach (get_object_vars($item) as $k => $v)
170 {
171 if (!$regex || preg_match($regex, $k))
172 {
173 if ($recurse)
174 {
175 $result[$k] = self::_fromObject($v, $recurse, $regex);
176 }
177 else
178 {
179 $result[$k] = $v;
180 }
181 }
182 }
183 }
184 elseif (is_array($item))
185 {
186 $result = array();
187
188 foreach ($item as $k => $v)
189 {
190 $result[$k] = self::_fromObject($v, $recurse, $regex);
191 }
192 }
193 else
194 {
195 $result = $item;
196 }
197
198 return $result;
199 }
200
201 /**
202 * Extracts a column from an array of arrays or objects
203 *
204 * @param array &$array The source array
205 * @param string $index The index of the column or name of object property
206 *
207 * @return array Column of values from the source array
208 *
209 * @since 11.1
210 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::getColumn instead
211 */
212 public static function getColumn(&$array, $index)
213 {
214 $result = array();
215
216 if (is_array($array))
217 {
218 $result = ArrayHelper::getColumn($array, $index);
219 }
220 else
221 {
222 JLog::add('This method is typehinted to be an array in \Joomla\Utilities\ArrayHelper::getColumn.', JLog::WARNING, 'deprecated');
223 }
224
225 return $result;
226 }
227
228 /**
229 * Utility function to return a value from a named array or a specified default
230 *
231 * @param array &$array A named array
232 * @param string $name The key to search for
233 * @param mixed $default The default value to give if no key found
234 * @param string $type Return type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY)
235 *
236 * @return mixed The value from the source array
237 *
238 * @since 11.1
239 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::getValue instead
240 */
241 public static function getValue(&$array, $name, $default = null, $type = '')
242 {
243 // Previously we didn't typehint an array. So force any object to be an array
244 return ArrayHelper::getValue((array) $array, $name, $default, $type);
245 }
246
247 /**
248 * Takes an associative array of arrays and inverts the array keys to values using the array values as keys.
249 *
250 * Example:
251 * $input = array(
252 * 'New' => array('1000', '1500', '1750'),
253 * 'Used' => array('3000', '4000', '5000', '6000')
254 * );
255 * $output = JArrayHelper::invert($input);
256 *
257 * Output would be equal to:
258 * $output = array(
259 * '1000' => 'New',
260 * '1500' => 'New',
261 * '1750' => 'New',
262 * '3000' => 'Used',
263 * '4000' => 'Used',
264 * '5000' => 'Used',
265 * '6000' => 'Used'
266 * );
267 *
268 * @param array $array The source array.
269 *
270 * @return array The inverted array.
271 *
272 * @since 12.3
273 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::invert instead
274 */
275 public static function invert($array)
276 {
277 return ArrayHelper::invert($array);
278 }
279
280 /**
281 * Method to determine if an array is an associative array.
282 *
283 * @param array $array An array to test.
284 *
285 * @return boolean True if the array is an associative array.
286 *
287 * @since 11.1
288 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::isAssociative instead
289 */
290 public static function isAssociative($array)
291 {
292 return ArrayHelper::isAssociative($array);
293 }
294
295 /**
296 * Pivots an array to create a reverse lookup of an array of scalars, arrays or objects.
297 *
298 * @param array $source The source array.
299 * @param string $key Where the elements of the source array are objects or arrays, the key to pivot on.
300 *
301 * @return array An array of arrays pivoted either on the value of the keys, or an individual key of an object or array.
302 *
303 * @since 11.3
304 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::pivot instead
305 */
306 public static function pivot($source, $key = null)
307 {
308 $result = array();
309
310 if (is_array($source))
311 {
312 $result = ArrayHelper::pivot($source, $key);
313 }
314 else
315 {
316 JLog::add('This method is typehinted to be an array in \Joomla\Utilities\ArrayHelper::pivot.', JLog::WARNING, 'deprecated');
317 }
318
319 return $result;
320 }
321
322 /**
323 * Utility function to sort an array of objects on a given field
324 *
325 * @param array &$a An array of objects
326 * @param mixed $k The key (string) or an array of keys to sort on
327 * @param mixed $direction Direction (integer) or an array of direction to sort in [1 = Ascending] [-1 = Descending]
328 * @param mixed $caseSensitive Boolean or array of booleans to let sort occur case sensitive or insensitive
329 * @param mixed $locale Boolean or array of booleans to let sort occur using the locale language or not
330 *
331 * @return array The sorted array of objects
332 *
333 * @since 11.1
334 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::sortObjects instead
335 */
336 public static function sortObjects(&$a, $k, $direction = 1, $caseSensitive = true, $locale = false)
337 {
338 if (!is_array($locale) || !is_array($locale[0]))
339 {
340 $locale = array($locale);
341 }
342
343 self::$sortCase = (array) $caseSensitive;
344 self::$sortDirection = (array) $direction;
345 self::$sortKey = (array) $k;
346 self::$sortLocale = $locale;
347
348 usort($a, array(__CLASS__, '_sortObjects'));
349
350 self::$sortCase = null;
351 self::$sortDirection = null;
352 self::$sortKey = null;
353 self::$sortLocale = null;
354
355 return $a;
356 }
357
358 /**
359 * Callback function for sorting an array of objects on a key
360 *
361 * @param array &$a An array of objects
362 * @param array &$b An array of objects
363 *
364 * @return integer Comparison status
365 *
366 * @see JArrayHelper::sortObjects()
367 * @since 11.1
368 */
369 protected static function _sortObjects(&$a, &$b)
370 {
371 $key = self::$sortKey;
372
373 for ($i = 0, $count = count($key); $i < $count; $i++)
374 {
375 if (isset(self::$sortDirection[$i]))
376 {
377 $direction = self::$sortDirection[$i];
378 }
379
380 if (isset(self::$sortCase[$i]))
381 {
382 $caseSensitive = self::$sortCase[$i];
383 }
384
385 if (isset(self::$sortLocale[$i]))
386 {
387 $locale = self::$sortLocale[$i];
388 }
389
390 $va = $a->{$key[$i]};
391 $vb = $b->{$key[$i]};
392
393 if ((is_bool($va) || is_numeric($va)) && (is_bool($vb) || is_numeric($vb)))
394 {
395 $cmp = $va - $vb;
396 }
397 elseif ($caseSensitive)
398 {
399 $cmp = StringHelper::strcmp($va, $vb, $locale);
400 }
401 else
402 {
403 $cmp = StringHelper::strcasecmp($va, $vb, $locale);
404 }
405
406 if ($cmp > 0)
407 {
408 return $direction;
409 }
410
411 if ($cmp < 0)
412 {
413 return -$direction;
414 }
415 }
416
417 return 0;
418 }
419
420 /**
421 * Multidimensional array safe unique test
422 *
423 * @param array $myArray The array to make unique.
424 *
425 * @return array
426 *
427 * @link https://secure.php.net/manual/en/function.array-unique.php
428 * @since 11.2
429 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::arrayUnique instead
430 */
431 public static function arrayUnique($myArray)
432 {
433 return is_array($myArray) ? ArrayHelper::arrayUnique($myArray) : $myArray;
434 }
435 }
436