batufa/system/classes/arr.php

155 lines
3.4 KiB
PHP

<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Array helper.
*
* @package Kohana
* @author Kohana Team
* @copyright (c) 2008-2009 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class arr_core {
/**
* Tests if an array is associative or or not.
*
* @param 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
// be associative.
return array_keys($keys) !== $keys;
}
/**
* 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)
{
$found = array();
foreach ($keys as $key)
{
$found[$key] = isset($array[$key]) ? $array[$key] : $default;
}
return $found;
}
/**
* 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)
{
$array = array_reverse($array, TRUE);
$array[$key] = $val;
$array = array_reverse($array, TRUE);
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++)
{
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;
}
}
}
return $result;
}
/**
* 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 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);
}
} // End arr