/
Map.php
51 lines (48 loc) · 1.33 KB
/
Map.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace x3\Functional;
class Map
{
/**
* Return a closure that calls `$method` on the object passed to it
* Mostly used for common *map operations
*
* @param string $method The method to call
*
* @return closure Calls `$method` on passed object
*/
public static function method($method)
{
$args = array_slice(func_get_args(), 1);
return function ($object) use ($method, $args) {
return call_user_func_array([$object, $method], $args);
};
}
/**
* Return a closure that returns `$attribute` of the object passed to it
* Mostly used for common *map operations
*
* @param string $attribute The attribute to reutrn
*
* @return closure Returns `$attribute` from passed object
*/
public static function attribute($attribute)
{
return function ($object) use ($attribute) {
return $object->$attribute;
};
}
/**
* Return a closure that returns the value of `$key` for the passed array
* Mostly used for common *map operations
*
* @param string $key The key
*
* @return closure Returns value of `$key` on passed array
*/
public static function key($key)
{
return function ($array) use ($key) {
return $array[$key];
};
}
}