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