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