Files
spa-api/laravel/arr.php

130 lines
3.0 KiB
PHP
Raw Normal View History

2011-09-21 22:00:06 -05:00
<?php namespace Laravel; use Closure;
2011-09-20 21:28:19 -05:00
class Arr {
/**
* Get an item from an array.
*
2011-09-21 22:00:06 -05:00
* "Dot" notation may be used to dig deep into the array.
*
* <code>
* // Get the $array['user']['name'] value from the array
* $name = Arr::get($array, 'user.name');
*
* // Return a default from if the specified item doesn't exist
* $name = Arr::get($array, 'user.name', 'Taylor');
* </code>
*
* @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-20 21:28:19 -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-21 21:46:16 -05:00
* The same "dot" syntax used by the "get" method may be used here.
2011-08-15 00:16:37 -05:00
*
* If no key is given to the method, the entire array will be replaced.
*
* <code>
* // Set the $array['user']['name'] value on the array
* Arr::set($array, 'user.name', 'Taylor');
* </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.
*
* <code>
* // Return the first array element that equals "Taylor"
* $value = Arr::first($array, function($k, $v) {return $v === 'Taylor';});
*
* // Return a default value if no matching element is found
* $value = Arr::first($array, function($k, $v) {return $v === 'Taylor'}, '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-20 21:28:19 -05:00
return ($default instanceof Closure) ? call_user_func($default) : $default;
2011-09-13 21:47:25 -05:00
}
2011-09-19 21:33:25 -05:00
/**
2011-09-21 21:46:16 -05:00
* Remove all array values that are contained within a given array of values.
2011-09-19 21:33:25 -05:00
*
* <code>
* // Remove all array values that are empty strings
* $array = Arr::without($array, '');
*
* // Remove all array values that are "One", "Two", or "Three"
* $array = Arr::without($array, array('One', 'Two', 'Three'));
* </code>
*
2011-09-19 21:33:25 -05:00
* @param array $array
* @param array $without
* @return array
*/
public static function without($array, $without = array())
{
2011-10-15 14:04:11 -05:00
$without = (array) $without;
foreach ((array) $array as $key => $value)
2011-09-19 21:33:25 -05:00
{
if (in_array($value, $without)) unset($array[$key]);
}
return $array;
}
}