/libraries/joomla/utilities/arrayhelper.php

https://github.com/shafiqissani/Jewelery-Ecommerce- · PHP · 286 lines · 170 code · 21 blank · 95 comment · 27 complexity · 49bc1480038f7aaa42a6c2456125d31e MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: arrayhelper.php 10707 2008-08-21 09:52:47Z eddieajau $
  4. * @package Joomla.Framework
  5. * @subpackage Utilities
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. /**
  15. * JArrayHelper is an array utility class for doing all sorts of odds and ends with arrays.
  16. *
  17. * @static
  18. * @package Joomla.Framework
  19. * @subpackage Utilities
  20. * @since 1.5
  21. */
  22. class JArrayHelper
  23. {
  24. /**
  25. * Function to convert array to integer values
  26. *
  27. * @static
  28. * @param array $array The source array to convert
  29. * @param mixed $default A default value (int|array) to assign if $array is not an array
  30. * @since 1.5
  31. */
  32. function toInteger(&$array, $default = null)
  33. {
  34. if (is_array($array)) {
  35. foreach ($array as $i => $v) {
  36. $array[$i] = (int) $v;
  37. }
  38. } else {
  39. if ($default === null) {
  40. $array = array();
  41. } elseif (is_array($default)) {
  42. JArrayHelper::toInteger($default, null);
  43. $array = $default;
  44. } else {
  45. $array = array( (int) $default );
  46. }
  47. }
  48. }
  49. /**
  50. * Utility function to map an array to a stdClass object.
  51. *
  52. * @static
  53. * @param array $array The array to map.
  54. * @param string $calss Name of the class to create
  55. * @return object The object mapped from the given array
  56. * @since 1.5
  57. */
  58. function toObject(&$array, $class = 'stdClass')
  59. {
  60. $obj = null;
  61. if (is_array($array))
  62. {
  63. $obj = new $class();
  64. foreach ($array as $k => $v)
  65. {
  66. if (is_array($v)) {
  67. $obj->$k = JArrayHelper::toObject($v, $class);
  68. } else {
  69. $obj->$k = $v;
  70. }
  71. }
  72. }
  73. return $obj;
  74. }
  75. function toString( $array = null, $inner_glue = '=', $outer_glue = ' ', $keepOuterKey = false )
  76. {
  77. $output = array();
  78. if (is_array($array))
  79. {
  80. foreach ($array as $key => $item)
  81. {
  82. if (is_array ($item))
  83. {
  84. if ($keepOuterKey) {
  85. $output[] = $key;
  86. }
  87. // This is value is an array, go and do it again!
  88. $output[] = JArrayHelper::toString( $item, $inner_glue, $outer_glue, $keepOuterKey);
  89. }
  90. else {
  91. $output[] = $key.$inner_glue.'"'.$item.'"';
  92. }
  93. }
  94. }
  95. return implode( $outer_glue, $output);
  96. }
  97. /**
  98. * Utility function to map an object to an array
  99. *
  100. * @static
  101. * @param object The source object
  102. * @param boolean True to recurve through multi-level objects
  103. * @param string An optional regular expression to match on field names
  104. * @return array The array mapped from the given object
  105. * @since 1.5
  106. */
  107. function fromObject( $p_obj, $recurse = true, $regex = null )
  108. {
  109. $result = null;
  110. if (is_object( $p_obj ))
  111. {
  112. $result = array();
  113. foreach (get_object_vars($p_obj) as $k => $v)
  114. {
  115. if ($regex)
  116. {
  117. if (!preg_match( $regex, $k ))
  118. {
  119. continue;
  120. }
  121. }
  122. if (is_object( $v ))
  123. {
  124. if ($recurse)
  125. {
  126. $result[$k] = JArrayHelper::fromObject( $v, $recurse, $regex );
  127. }
  128. }
  129. else
  130. {
  131. $result[$k] = $v;
  132. }
  133. }
  134. }
  135. return $result;
  136. }
  137. /**
  138. * Extracts a column from an array of arrays or objects
  139. *
  140. * @static
  141. * @param array $array The source array
  142. * @param string $index The index of the column or name of object property
  143. * @return array Column of values from the source array
  144. * @since 1.5
  145. */
  146. function getColumn(&$array, $index)
  147. {
  148. $result = array ();
  149. if (is_array($array))
  150. {
  151. $n = count($array);
  152. for ($i = 0; $i < $n; $i++)
  153. {
  154. $item = & $array[$i];
  155. if (is_array($item) && isset ($item[$index])) {
  156. $result[] = $item[$index];
  157. } elseif (is_object($item) && isset ($item-> $index)) {
  158. $result[] = $item-> $index;
  159. }
  160. // else ignore the entry
  161. }
  162. }
  163. return $result;
  164. }
  165. /**
  166. * Utility function to return a value from a named array or a specified default
  167. *
  168. * @static
  169. * @param array $array A named array
  170. * @param string $name The key to search for
  171. * @param mixed $default The default value to give if no key found
  172. * @param string $type Return type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY)
  173. * @return mixed The value from the source array
  174. * @since 1.5
  175. */
  176. function getValue(&$array, $name, $default=null, $type='')
  177. {
  178. // Initialize variables
  179. $result = null;
  180. if (isset ($array[$name])) {
  181. $result = $array[$name];
  182. }
  183. // Handle the default case
  184. if (is_null($result)) {
  185. $result = $default;
  186. }
  187. // Handle the type constraint
  188. switch (strtoupper($type))
  189. {
  190. case 'INT' :
  191. case 'INTEGER' :
  192. // Only use the first integer value
  193. @ preg_match('/-?[0-9]+/', $result, $matches);
  194. $result = @ (int) $matches[0];
  195. break;
  196. case 'FLOAT' :
  197. case 'DOUBLE' :
  198. // Only use the first floating point value
  199. @ preg_match('/-?[0-9]+(\.[0-9]+)?/', $result, $matches);
  200. $result = @ (float) $matches[0];
  201. break;
  202. case 'BOOL' :
  203. case 'BOOLEAN' :
  204. $result = (bool) $result;
  205. break;
  206. case 'ARRAY' :
  207. if (!is_array($result)) {
  208. $result = array ($result);
  209. }
  210. break;
  211. case 'STRING' :
  212. $result = (string) $result;
  213. break;
  214. case 'WORD' :
  215. $result = (string) preg_replace( '#\W#', '', $result );
  216. break;
  217. case 'NONE' :
  218. default :
  219. // No casting necessary
  220. break;
  221. }
  222. return $result;
  223. }
  224. /**
  225. * Utility function to sort an array of objects on a given field
  226. *
  227. * @static
  228. * @param array $arr An array of objects
  229. * @param string $k The key to sort on
  230. * @param int $direction Direction to sort in [1 = Ascending] [-1 = Descending]
  231. * @return array The sorted array of objects
  232. * @since 1.5
  233. */
  234. function sortObjects( &$a, $k, $direction=1 )
  235. {
  236. $GLOBALS['JAH_so'] = array(
  237. 'key' => $k,
  238. 'direction' => $direction
  239. );
  240. usort( $a, array('JArrayHelper', '_sortObjects') );
  241. unset( $GLOBALS['JAH_so'] );
  242. return $a;
  243. }
  244. /**
  245. * Private callback function for sorting an array of objects on a key
  246. *
  247. * @static
  248. * @param array $a An array of objects
  249. * @param array $b An array of objects
  250. * @return int Comparison status
  251. * @since 1.5
  252. * @see JArrayHelper::sortObjects()
  253. */
  254. function _sortObjects( &$a, &$b )
  255. {
  256. $params = $GLOBALS['JAH_so'];
  257. if ( $a->$params['key'] > $b->$params['key'] ) {
  258. return $params['direction'];
  259. }
  260. if ( $a->$params['key'] < $b->$params['key'] ) {
  261. return -1 * $params['direction'];
  262. }
  263. return 0;
  264. }
  265. }