Files
spa-api/laravel/arr.php

113 lines
2.9 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
class Arr {
/**
* Get an item from an array.
*
2011-09-16 19:59:20 -05:00
* This method supports accessing arrays through JavaScript "dot" style syntax
* for conveniently digging deep into nested arrays. Like most other Laravel
* "get" methods, a default value may be provided.
*
* <code>
* // Get the value of $array['user']['name']
* $value = Arr::get($array, 'user.name');
*
* // Get a value from the array, but return a default if it doesn't exist
* $value = Arr::get($array, 'user.name', 'Taylor');
* </code>
2011-08-09 14:31:57 -05:00
*
* @param array $array
2011-06-16 22:27:57 -05:00
* @param string $key
* @param mixed $default
* @return mixed
*/
2011-06-16 22:27:57 -05:00
public static function get($array, $key, $default = null)
{
2011-08-08 08:27:12 -05:00
if (is_null($key)) return $array;
2011-08-02 13:38:50 -05:00
foreach (explode('.', $key) as $segment)
{
2011-08-15 15:05:57 -05:00
if ( ! is_array($array) or ! array_key_exists($segment, $array))
2011-08-02 13:38:50 -05:00
{
2011-09-16 19:59:20 -05:00
return ($default instanceof \Closure) ? call_user_func($default) : $default;
2011-08-02 13:38:50 -05:00
}
$array = $array[$segment];
}
return $array;
}
2011-08-15 00:16:37 -05:00
/**
2011-08-15 10:38:02 -05:00
* Set an array item to a given value.
2011-08-15 00:16:37 -05:00
*
2011-09-16 19:59:20 -05:00
* This method supports accessing arrays through JavaScript "dot" style syntax
* for conveniently digging deep into nested arrays.
*
* <code>
* // Set the $array['user']['name'] value in the array
* Arr::set($array, 'user.name', 'Taylor');
*
* // Set the $array['db']['driver']['name'] value in the array
* Arr::set($array, 'db.driver.name', 'SQLite');
* </code>
2011-08-15 00:16:37 -05:00
*
* @param array $array
* @param string $key
* @param mixed $value
* @return void
*/
public static function set(&$array, $key, $value)
{
if (is_null($key)) return $array = $value;
$keys = explode('.', $key);
2011-08-15 00:16:37 -05:00
while (count($keys) > 1)
2011-08-15 00:16:37 -05:00
{
$key = array_shift($keys);
2011-08-15 15:05:57 -05:00
if ( ! isset($array[$key]) or ! is_array($array[$key]))
2011-08-15 00:16:37 -05:00
{
$array[$key] = array();
2011-08-15 00:16:37 -05:00
}
$array =& $array[$key];
2011-08-15 00:16:37 -05:00
}
$array[array_shift($keys)] = $value;
2011-08-15 00:16:37 -05:00
}
2011-09-13 21:47:25 -05:00
/**
* Return the first element in an array which passes a given truth test.
*
2011-09-16 19:59:20 -05:00
* The truth test is passed as a closure, and simply returns true or false.
* The array key and value will be passed to the closure on each iteration.
*
* Like the "get" method, a default value may be specified, and will be
* returned if no matching array elements are found by the method.
*
* <code>
* // Get the first string from an array with a length of 3
* $value = Arr::first($array, function($k, $v) {return strlen($v) == 3;});
*
* // Return a default value if no matching array elements are found
* $value = Arr::first($array, function($k, $v) {return;}, 'Default');
* </code>
*
2011-09-13 21:47:25 -05:00
* @param array $array
* @param Closure $callback
* @return mixed
*/
2011-09-15 22:27:29 -05:00
public static function first($array, $callback, $default = null)
2011-09-13 21:47:25 -05:00
{
foreach ($array as $key => $value)
{
if (call_user_func($callback, $key, $value)) return $value;
}
2011-09-15 22:27:29 -05:00
2011-09-16 19:59:20 -05:00
return ($default instanceof \Closure) ? call_user_func($default) : $default;
2011-09-13 21:47:25 -05:00
}
}