2015-10-05 16:54:25 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aviat\Ion\Type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper class for native array methods for convenience
|
2015-10-09 22:29:59 -04:00
|
|
|
*
|
|
|
|
* @method array chunk(int $size, bool $preserve_keys = FALSE)
|
|
|
|
* @method array pluck(mixed $column_key, mixed $index_key = NULL)
|
|
|
|
* @method array filter(callable $callback = NULL, int $flag = 0)
|
2015-10-05 16:54:25 -04:00
|
|
|
*/
|
|
|
|
class ArrayType {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current array
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $arr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map generated methods to their native implementations
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $native_methods = [
|
|
|
|
'chunk' => 'array_chunk',
|
|
|
|
'pluck' => 'array_column',
|
|
|
|
'key_diff' => 'array_diff_key',
|
|
|
|
'diff' => 'array_diff',
|
|
|
|
'filter' => 'array_filter',
|
|
|
|
'flip' => 'array_flip',
|
|
|
|
'intersect' => 'array_intersect',
|
|
|
|
'keys' => 'array_keys',
|
|
|
|
'merge' => 'array_merge',
|
|
|
|
'pad' => 'array_pad',
|
|
|
|
'product' => 'array_product',
|
|
|
|
'random' => 'array_rand',
|
|
|
|
'reduce' => 'array_reduce',
|
|
|
|
'reverse' => 'array_reverse',
|
|
|
|
'sum' => 'array_sum',
|
|
|
|
'unique' => 'array_unique',
|
|
|
|
'values' => 'array_values',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Native methods that modify the passed in array
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $native_in_place_methods = [
|
|
|
|
'shuffle' => 'shuffle',
|
2015-10-09 22:29:59 -04:00
|
|
|
'shift' => 'array_shift',
|
|
|
|
'unshift' => 'array_unshift',
|
|
|
|
'push' => 'array_push',
|
|
|
|
'pop' => 'array_pop',
|
2015-10-05 16:54:25 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an ArrayType wrapper class
|
|
|
|
*
|
|
|
|
* @param array $arr
|
|
|
|
*/
|
2015-10-15 09:25:30 -04:00
|
|
|
public function __construct(array &$arr)
|
2015-10-05 16:54:25 -04:00
|
|
|
{
|
|
|
|
$this->arr =& $arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call one of the dynamically created methods
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param array $args
|
|
|
|
* @return mixed
|
2015-10-09 22:29:59 -04:00
|
|
|
* @throws \InvalidArgumentException
|
2015-10-05 16:54:25 -04:00
|
|
|
*/
|
|
|
|
public function __call($method, $args)
|
|
|
|
{
|
|
|
|
// Simple mapping for the majority of methods
|
|
|
|
if (array_key_exists($method, $this->native_methods))
|
|
|
|
{
|
|
|
|
$func = $this->native_methods[$method];
|
|
|
|
// Set the current array as the first argument of the method
|
|
|
|
array_unshift($args, $this->arr);
|
|
|
|
return call_user_func_array($func, $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mapping for in-place methods
|
|
|
|
if (array_key_exists($method, $this->native_in_place_methods))
|
|
|
|
{
|
|
|
|
$func = $this->native_in_place_methods[$method];
|
|
|
|
$func($this->arr);
|
|
|
|
return $this->arr;
|
|
|
|
}
|
2015-10-09 22:29:59 -04:00
|
|
|
|
|
|
|
throw new \InvalidArgumentException("Method '{$method}' does not exist");
|
2015-10-05 16:54:25 -04:00
|
|
|
}
|
2015-10-09 22:29:59 -04:00
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
/**
|
|
|
|
* Does the passed key exist in the current array?
|
|
|
|
*
|
2015-10-09 14:55:15 -04:00
|
|
|
* @param string $key
|
2015-10-09 14:34:55 -04:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has_key($key)
|
|
|
|
{
|
|
|
|
return array_key_exists($key, $this->arr);
|
|
|
|
}
|
2015-10-05 16:54:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill an array with the specified value
|
|
|
|
*
|
|
|
|
* @param int $start_index
|
|
|
|
* @param int $num
|
|
|
|
* @param mixed $value
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fill($start_index, $num, $value)
|
|
|
|
{
|
|
|
|
return array_fill($start_index, $num, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call a callback on each item of the array
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function map(callable $callback)
|
|
|
|
{
|
|
|
|
return array_map($callback, $this->arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find an array key by its associated value
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @param bool $strict
|
2015-10-12 14:27:20 -04:00
|
|
|
* @return false|integer|string
|
2015-10-05 16:54:25 -04:00
|
|
|
*/
|
|
|
|
public function search($value, $strict=FALSE)
|
|
|
|
{
|
|
|
|
return array_search($value, $this->arr, $strict);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the array has the passed value
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @param bool $strict
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has($value, $strict=FALSE)
|
|
|
|
{
|
|
|
|
return in_array($value, $this->arr, $strict);
|
|
|
|
}
|
2015-10-15 09:25:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return $this->arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a reference to the value of an arbitrary key on the array
|
|
|
|
*
|
|
|
|
* @param array $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function &get_deep_key(array $key)
|
|
|
|
{
|
|
|
|
$pos =& $this->arr;
|
|
|
|
|
|
|
|
// Create the start of the array if it doesn't exist
|
|
|
|
if ( ! is_array($pos))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the levels of the array,
|
|
|
|
// create the levels if they don't exist
|
|
|
|
foreach($key as $level)
|
|
|
|
{
|
|
|
|
$pos =& $pos[$level];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pos;
|
|
|
|
}
|
2015-10-05 16:54:25 -04:00
|
|
|
}
|
|
|
|
// End of ArrayType.php
|