2009-02-28 13:40:47 +02:00
|
|
|
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
2009-03-21 00:01:47 +02:00
|
|
|
/**
|
|
|
|
* Array helper.
|
|
|
|
*
|
|
|
|
* @package Kohana
|
|
|
|
* @author Kohana Team
|
|
|
|
* @copyright (c) 2008-2009 Kohana Team
|
|
|
|
* @license http://kohanaphp.com/license.html
|
|
|
|
*/
|
2009-02-28 13:40:47 +02:00
|
|
|
class arr_core {
|
|
|
|
|
2009-05-20 20:29:53 +03:00
|
|
|
/**
|
|
|
|
* Tests if an array is associative or or not.
|
|
|
|
*
|
|
|
|
* @param array array to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-02-28 13:40:47 +02:00
|
|
|
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
|
|
|
|
// be associative.
|
|
|
|
return array_keys($keys) !== $keys;
|
|
|
|
}
|
|
|
|
|
2009-05-20 20:29:53 +03:00
|
|
|
/**
|
|
|
|
* Retrieves multiple keys from an array. If the key does not exist in the
|
|
|
|
* array, the default value will be added instead (NULL by default).
|
|
|
|
*
|
|
|
|
* @param array array to extract keys from
|
|
|
|
* @param array list of keys
|
|
|
|
* @param mixed default value
|
|
|
|
*/
|
|
|
|
public static function extract(array $array, array $keys, $default = NULL)
|
2009-02-28 13:40:47 +02:00
|
|
|
{
|
2009-05-20 20:29:53 +03:00
|
|
|
$found = array();
|
|
|
|
foreach ($keys as $key)
|
|
|
|
{
|
|
|
|
$found[$key] = isset($array[$key]) ? $array[$key] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2009-02-28 13:40:47 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 20:29:53 +03:00
|
|
|
/**
|
|
|
|
* Adds a value to the beginning of an associative array.
|
|
|
|
*
|
|
|
|
* @param array array to modify
|
|
|
|
* @param string array key name
|
|
|
|
* @param mixed array value
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function unshift( array & $array, $key, $val)
|
2009-02-28 13:40:47 +02:00
|
|
|
{
|
2009-05-20 20:29:53 +03:00
|
|
|
$array = array_reverse($array, TRUE);
|
|
|
|
$array[$key] = $val;
|
|
|
|
$array = array_reverse($array, TRUE);
|
2009-02-28 13:40:47 +02:00
|
|
|
|
2009-05-20 20:29:53 +03:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges one or more arrays recursively and preserves all keys.
|
|
|
|
* Note that this does not work the same the PHP function array_merge_recursive()!
|
|
|
|
*
|
|
|
|
* @param array initial array
|
|
|
|
* @param array array to merge
|
|
|
|
* @param array ...
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function merge(array $a1)
|
|
|
|
{
|
|
|
|
$result = array();
|
|
|
|
for ($i = 0, $total = func_num_args(); $i < $total; $i++)
|
2009-02-28 13:40:47 +02:00
|
|
|
{
|
2009-05-20 20:29:53 +03:00
|
|
|
foreach (func_get_arg($i) as $key => $val)
|
|
|
|
{
|
|
|
|
if (isset($result[$key]))
|
|
|
|
{
|
|
|
|
if (is_array($val))
|
|
|
|
{
|
|
|
|
// Arrays are merged recursively
|
|
|
|
$result[$key] = arr::merge($result[$key], $val);
|
|
|
|
}
|
|
|
|
elseif (is_int($key))
|
|
|
|
{
|
|
|
|
// Indexed arrays are appended
|
|
|
|
array_push($result, $val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Associative arrays are replaced
|
|
|
|
$result[$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// New values are added
|
|
|
|
$result[$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
2009-02-28 13:40:47 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 20:29:53 +03:00
|
|
|
return $result;
|
2009-02-28 13:40:47 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 20:29:53 +03:00
|
|
|
/**
|
|
|
|
* Creates a callable function and parameter list from a string representation.
|
2009-05-30 13:23:39 +03:00
|
|
|
* Note that this function does not validate the callback string.
|
2009-05-20 20:29:53 +03:00
|
|
|
*
|
|
|
|
* // Get the callback function and parameters
|
2009-05-30 12:08:46 +03:00
|
|
|
* list($func, $params) = arr::callback('foo::bar[apple,orange]');
|
2009-05-20 20:29:53 +03:00
|
|
|
*
|
|
|
|
* // Get the result of the callback
|
|
|
|
* $result = call_user_func_array($func, $params);
|
|
|
|
*
|
|
|
|
* @param string callback string
|
|
|
|
* @return array function, params
|
|
|
|
*/
|
|
|
|
public static function callback($str)
|
2009-02-28 13:40:47 +02:00
|
|
|
{
|
2009-05-20 20:29:53 +03:00
|
|
|
// Overloaded as parts are found
|
|
|
|
$command = $params = NULL;
|
|
|
|
|
|
|
|
// command[param,param]
|
2009-05-30 13:23:39 +03:00
|
|
|
if (preg_match('/^([^\[]*+)\[(.*)\]$/', $str, $match))
|
2009-05-20 20:29:53 +03:00
|
|
|
{
|
|
|
|
// command
|
|
|
|
$command = $match[1];
|
|
|
|
|
2009-05-30 13:23:39 +03:00
|
|
|
if ($match[2] !== '')
|
2009-05-20 20:29:53 +03:00
|
|
|
{
|
|
|
|
// 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);
|
2009-02-28 13:40:47 +02:00
|
|
|
}
|
|
|
|
|
2009-05-30 13:23:39 +03:00
|
|
|
} // End arr
|