/usr/share/php/kohana3.1/system/classes/kohana/arr.php is in libkohana3.1-core-php 3.1.5-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | <?php defined('SYSPATH') OR die('No direct script access.');
/**
* Array helper.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Arr {
/**
* @var string default delimiter for path()
*/
public static $delimiter = '.';
/**
* Tests if an array is associative or not.
*
* // Returns TRUE
* Arr::is_assoc(array('username' => 'john.doe'));
*
* // Returns FALSE
* Arr::is_assoc('foo', 'bar');
*
* @param array $array array to check
* @return boolean
*/
public static function is_assoc(array $array)
{
// Keys of the array
$keys = array_keys($array);
// If the array keys of the keys match the keys, then the array must
// not be associative (e.g. the keys array looked like {0:0, 1:1...}).
return array_keys($keys) !== $keys;
}
/**
* Test if a value is an array with an additional check for array-like objects.
*
* // Returns TRUE
* Arr::is_array(array());
* Arr::is_array(new ArrayObject);
*
* // Returns FALSE
* Arr::is_array(FALSE);
* Arr::is_array('not an array!');
* Arr::is_array(Database::instance());
*
* @param mixed $value value to check
* @return boolean
*/
public static function is_array($value)
{
if (is_array($value))
{
// Definitely an array
return TRUE;
}
else
{
// Possibly a Traversable object, functionally the same as an array
return (is_object($value) AND $value instanceof Traversable);
}
}
/**
* Gets a value from an array using a dot separated path.
*
* // Get the value of $array['foo']['bar']
* $value = Arr::path($array, 'foo.bar');
*
* Using a wildcard "*" will search intermediate arrays and return an array.
*
* // Get the values of "color" in theme
* $colors = Arr::path($array, 'theme.*.color');
*
* // Using an array of keys
* $colors = Arr::path($array, array('theme', '*', 'color'));
*
* @param array $array array to search
* @param mixed $path key path string (delimiter separated) or array of keys
* @param mixed $default default value if the path is not set
* @param string $delimiter key path delimiter
* @return mixed
*/
public static function path($array, $path, $default = NULL, $delimiter = NULL)
{
if ( ! Arr::is_array($array))
{
// This is not an array!
return $default;
}
if (is_array($path))
{
// The path has already been separated into keys
$keys = $path;
}
else
{
if (array_key_exists($path, $array))
{
// No need to do extra processing
return $array[$path];
}
if ($delimiter === NULL)
{
// Use the default delimiter
$delimiter = Arr::$delimiter;
}
// Remove starting delimiters and spaces
$path = ltrim($path, "{$delimiter} ");
// Remove ending delimiters, spaces, and wildcards
$path = rtrim($path, "{$delimiter} *");
// Split the keys by delimiter
$keys = explode($delimiter, $path);
}
do
{
$key = array_shift($keys);
if (ctype_digit($key))
{
// Make the key an integer
$key = (int) $key;
}
if (isset($array[$key]))
{
if ($keys)
{
if (Arr::is_array($array[$key]))
{
// Dig down into the next part of the path
$array = $array[$key];
}
else
{
// Unable to dig deeper
break;
}
}
else
{
// Found the path requested
return $array[$key];
}
}
elseif ($key === '*')
{
// Handle wildcards
$values = array();
foreach ($array as $arr)
{
if ($value = Arr::path($arr, implode('.', $keys)))
{
$values[] = $value;
}
}
if ($values)
{
// Found the values requested
return $values;
}
else
{
// Unable to dig deeper
break;
}
}
else
{
// Unable to dig deeper
break;
}
}
while ($keys);
// Unable to find the value requested
return $default;
}
/**
* Set a value on an array by path.
*
* @see Arr::path()
* @param array $array Array to update
* @param string $path Path
* @param mixed $value Value to set
* @param string $delimiter Path delimiter
*/
public static function set_path( & $array, $path, $value, $delimiter = NULL)
{
if ( ! $delimiter)
{
// Use the default delimiter
$delimiter = Arr::$delimiter;
}
// Split the keys by delimiter
$keys = explode($delimiter, $path);
// Set current $array to inner-most array path
while (count($keys) > 1)
{
$key = array_shift($keys);
if (ctype_digit($key))
{
// Make the key an integer
$key = (int) $key;
}
if ( ! isset($array[$key]))
{
$array[$key] = array();
}
$array = & $array[$key];
}
// Set key on inner-most array
$array[array_shift($keys)] = $value;
}
/**
* Fill an array with a range of numbers.
*
* // Fill an array with values 5, 10, 15, 20
* $values = Arr::range(5, 20);
*
* @param integer $step stepping
* @param integer $max ending number
* @return array
*/
public static function range($step = 10, $max = 100)
{
if ($step < 1)
return array();
$array = array();
for ($i = $step; $i <= $max; $i += $step)
{
$array[$i] = $i;
}
return $array;
}
/**
* Retrieve a single key from an array. If the key does not exist in the
* array, the default value will be returned instead.
*
* // Get the value "username" from $_POST, if it exists
* $username = Arr::get($_POST, 'username');
*
* // Get the value "sorting" from $_GET, if it exists
* $sorting = Arr::get($_GET, 'sorting');
*
* @param array $array array to extract from
* @param string $key key name
* @param mixed $default default value
* @return mixed
*/
public static function get($array, $key, $default = NULL)
{
return isset($array[$key]) ? $array[$key] : $default;
}
/**
* Retrieves multiple keys from an array. If the key does not exist in the
* array, the default value will be added instead.
*
* // Get the values "username", "password" from $_POST
* $auth = Arr::extract($_POST, array('username', 'password'));
*
* @param array $array array to extract keys from
* @param array $keys list of key names
* @param mixed $default default value
* @return array
*/
public static function extract($array, array $keys, $default = NULL)
{
$found = array();
foreach ($keys as $key)
{
$found[$key] = isset($array[$key]) ? $array[$key] : $default;
}
return $found;
}
/**
* Retrieves muliple single-key values from a list of arrays.
*
* // Get all of the "id" values from a result
* $ids = Arr::pluck($result, 'id');
*
* [!!] A list of arrays is an array that contains arrays, eg: array(array $a, array $b, array $c, ...)
*
* @param array $array list of arrays to check
* @param string $key key to pluck
* @return array
*/
public static function pluck($array, $key)
{
$values = array();
foreach ($array as $row)
{
if (isset($row[$key]))
{
// Found a value in this row
$values[] = $row[$key];
}
}
return $values;
}
/**
* Adds a value to the beginning of an associative array.
*
* // Add an empty value to the start of a select list
* Arr::unshift($array, 'none', 'Select a value');
*
* @param array $array array to modify
* @param string $key array key name
* @param mixed $val array value
* @return array
*/
public static function unshift( array & $array, $key, $val)
{
$array = array_reverse($array, TRUE);
$array[$key] = $val;
$array = array_reverse($array, TRUE);
return $array;
}
/**
* Recursive version of [array_map](http://php.net/array_map), applies the
* same callback to all elements in an array, including sub-arrays.
*
* // Apply "strip_tags" to every element in the array
* $array = Arr::map('strip_tags', $array);
*
* [!!] Unlike `array_map`, this method requires a callback and will only map
* a single array.
*
* @param mixed $callback callback applied to every element in the array
* @param array $array array to map
* @return array
*/
public static function map($callback, $array)
{
foreach ($array as $key => $val)
{
if (is_array($val))
{
$array[$key] = Arr::map($callback, $val);
}
else
{
$array[$key] = call_user_func($callback, $val);
}
}
return $array;
}
/**
* Merges one or more arrays recursively and preserves all keys.
* Note that this does not work the same as [array_merge_recursive](http://php.net/array_merge_recursive)!
*
* $john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane'));
* $mary = array('name' => 'mary', 'children' => array('jane'));
*
* // John and Mary are married, merge them together
* $john = Arr::merge($john, $mary);
*
* // The output of $john will now be:
* array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane'))
*
* @param array $a1 initial array
* @param array $a2,... array to merge
* @return array
*/
public static function merge(array $a1, array $a2)
{
$result = array();
for ($i = 0, $total = func_num_args(); $i < $total; $i++)
{
// Get the next array
$arr = func_get_arg($i);
// Is the array associative?
$assoc = Arr::is_assoc($arr);
foreach ($arr as $key => $val)
{
if (isset($result[$key]))
{
if (is_array($val) AND is_array($result[$key]))
{
if (Arr::is_assoc($val))
{
// Associative arrays are merged recursively
$result[$key] = Arr::merge($result[$key], $val);
}
else
{
// Find the values that are not already present
$diff = array_diff($val, $result[$key]);
// Indexed arrays are merged to prevent duplicates
$result[$key] = array_merge($result[$key], $diff);
}
}
else
{
if ($assoc)
{
// Associative values are replaced
$result[$key] = $val;
}
elseif ( ! in_array($val, $result, TRUE))
{
// Indexed values are added only if they do not yet exist
$result[] = $val;
}
}
}
else
{
// New values are added
$result[$key] = $val;
}
}
}
return $result;
}
/**
* Overwrites an array with values from input arrays.
* Keys that do not exist in the first array will not be added!
*
* $a1 = array('name' => 'john', 'mood' => 'happy', 'food' => 'bacon');
* $a2 = array('name' => 'jack', 'food' => 'tacos', 'drink' => 'beer');
*
* // Overwrite the values of $a1 with $a2
* $array = Arr::overwrite($a1, $a2);
*
* // The output of $array will now be:
* array('name' => 'jack', 'mood' => 'happy', 'food' => 'tacos')
*
* @param array $array1 master array
* @param array $array2 input arrays that will overwrite existing values
* @return array
*/
public static function overwrite($array1, $array2)
{
foreach (array_intersect_key($array2, $array1) as $key => $value)
{
$array1[$key] = $value;
}
if (func_num_args() > 2)
{
foreach (array_slice(func_get_args(), 2) as $array2)
{
foreach (array_intersect_key($array2, $array1) as $key => $value)
{
$array1[$key] = $value;
}
}
}
return $array1;
}
/**
* Creates a callable function and parameter list from a string representation.
* Note that this function does not validate the callback string.
*
* // Get the callback function and parameters
* list($func, $params) = Arr::callback('Foo::bar(apple,orange)');
*
* // Get the result of the callback
* $result = call_user_func_array($func, $params);
*
* @param string $str callback string
* @return array function, params
*/
public static function callback($str)
{
// Overloaded as parts are found
$command = $params = NULL;
// command[param,param]
if (preg_match('/^([^\(]*+)\((.*)\)$/', $str, $match))
{
// command
$command = $match[1];
if ($match[2] !== '')
{
// param,param
$params = preg_split('/(?<!\\\\),/', $match[2]);
$params = str_replace('\,', ',', $params);
}
}
else
{
// command
$command = $str;
}
if (strpos($command, '::') !== FALSE)
{
// Create a static method callable command
$command = explode('::', $command, 2);
}
return array($command, $params);
}
/**
* Convert a multi-dimensional array into a single-dimensional array.
*
* $array = array('set' => array('one' => 'something'), 'two' => 'other');
*
* // Flatten the array
* $array = Arr::flatten($array);
*
* // The array will now be
* array('one' => 'something', 'two' => 'other');
*
* [!!] The keys of array values will be discarded.
*
* @param array $array array to flatten
* @return array
* @since 3.0.6
*/
public static function flatten($array)
{
$is_assoc = Arr::is_assoc($array);
$flat = array();
foreach ($array as $key => $value)
{
if (is_array($value))
{
$flat = array_merge($flat, Arr::flatten($value));
}
else
{
if ($is_assoc)
{
$flat[$key] = $value;
}
else
{
$flat[] = $value;
}
}
}
return $flat;
}
} // End arr
|