PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/joomla/utilities/arrayhelper.php

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