|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Underscore; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @method static Underscore from($item) |
|
7
|
|
|
* @method static Underscore range(int $start, int $stop, int $step = 1) |
|
8
|
|
|
* @method static Underscore times(int $count, int $function, int $step = 1) |
|
9
|
|
|
* |
|
10
|
|
|
* @method Underscore clone() |
|
11
|
|
|
* @method Underscore compact() |
|
12
|
|
|
* @method Underscore defaults(mixed $default, ...) |
|
13
|
|
|
* @method Underscore difference($values) |
|
14
|
|
|
* @method Underscore extend(mixed $source, ...) |
|
15
|
|
|
* @method Underscore filter(callable $iterator) |
|
16
|
|
|
* @method Underscore flatten() |
|
17
|
|
|
* @method Underscore groupBy(callable $iterator) |
|
18
|
|
|
* @method Underscore head(int $count = 1) |
|
19
|
|
|
* @method Underscore initial(int $count = 1) |
|
20
|
|
|
* @method Underscore invoke(callable $iterator) |
|
21
|
|
|
* @method Underscore keys() |
|
22
|
|
|
* @method Underscore last(int $count = 1) |
|
23
|
|
|
* @method Underscore map(callable $iterator) |
|
24
|
|
|
* @method Underscore pick(mixed $key) |
|
25
|
|
|
* @method Underscore merge($values) |
|
26
|
|
|
* @method Underscore intersection($values) |
|
27
|
|
|
* @method Underscore reject(callable $iterator) |
|
28
|
|
|
* @method Underscore shuffle() |
|
29
|
|
|
* @method Underscore sortBy(callable $iterator) |
|
30
|
|
|
* @method Underscore tail(int $count = 1) |
|
31
|
|
|
* @method Underscore tap(callable $iterator) |
|
32
|
|
|
* @method Underscore thru(callable $iterator) |
|
33
|
|
|
* @method Underscore uniq() |
|
34
|
|
|
* @method Underscore values() |
|
35
|
|
|
* @method Underscore where(array $properties, $strict = true) |
|
36
|
|
|
* @method Underscore without($values) |
|
37
|
|
|
* @method Underscore zip(array $keys) |
|
38
|
|
|
* |
|
39
|
|
|
* @method Collection collection() |
|
40
|
|
|
* @method bool all(callable $iterator) |
|
41
|
|
|
* @method bool any(callable $iterator) |
|
42
|
|
|
* @method bool contains(mixed $needle) |
|
43
|
|
|
* @method bool find(callable $iterator) |
|
44
|
|
|
* @method mixed indexOf(mixed $needle) |
|
45
|
|
|
* @method mixed lastIndexOf(mixed $needle) |
|
46
|
|
|
* @method mixed value() |
|
47
|
|
|
* @method mixed[] toArray() |
|
48
|
|
|
* @method int size() |
|
49
|
|
|
* @method mixed min() |
|
50
|
|
|
* @method mixed max() |
|
51
|
|
|
* @method boolean has($key) |
|
52
|
|
|
* @method mixed reduce(callable $iterator, mixed $initial = null) |
|
53
|
|
|
* @method mixed reduceRight(callable $iterator, mixed $initial = null) |
|
54
|
|
|
*/ |
|
55
|
|
|
class Underscore |
|
56
|
|
|
{ |
|
57
|
|
|
/** @var Collection */ |
|
58
|
|
|
protected $wrapped; |
|
59
|
|
|
|
|
60
|
|
|
/** @var array */ |
|
61
|
|
|
protected static $mixins = []; |
|
62
|
|
|
|
|
63
|
|
|
/** @var Registry */ |
|
64
|
|
|
protected static $registry; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set the registry singleton. |
|
68
|
|
|
* |
|
69
|
|
|
* @param Registry $registry |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public static function setRegistry(Registry $registry) |
|
73
|
|
|
{ |
|
74
|
1 |
|
static::$registry = $registry; |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the registry singleton. |
|
79
|
|
|
* |
|
80
|
|
|
* Creates a new registry if none exists. |
|
81
|
|
|
* |
|
82
|
|
|
* @return Registry |
|
83
|
|
|
*/ |
|
84
|
54 |
|
public static function getRegistry() |
|
85
|
|
|
{ |
|
86
|
54 |
|
if (!static::$registry) { |
|
87
|
1 |
|
static::$registry = new Registry; |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
54 |
|
return static::$registry; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Allows you to extend Underscore with your own utility functions. |
|
95
|
|
|
* |
|
96
|
|
|
* Pass a hash of {name: function} definitions to have your functions added |
|
97
|
|
|
* to the Underscore object, as well as the OOP wrapper. |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $functions |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public static function mixin(array $functions) |
|
103
|
|
|
{ |
|
104
|
1 |
|
foreach ($functions as $name => $function) { |
|
105
|
1 |
|
static::$registry->alias($name, $function); |
|
106
|
1 |
|
} |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param Collection $wrapped |
|
111
|
|
|
*/ |
|
112
|
52 |
|
public function __construct(Collection $wrapped = null) |
|
113
|
|
|
{ |
|
114
|
52 |
|
$this->wrapped = $wrapped; |
|
115
|
52 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $method |
|
119
|
|
|
* @param array $args |
|
120
|
|
|
* @throws \BadMethodCallException |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
52 |
|
public function __call($method, $args) |
|
124
|
|
|
{ |
|
125
|
52 |
|
return $this->executePayload(static::getRegistry()->instance($method), $args); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $method |
|
130
|
|
|
* @param array $args |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
53 |
|
public static function __callStatic($method, $args) |
|
134
|
|
|
{ |
|
135
|
53 |
|
return call_user_func_array(static::getRegistry()->instance($method), $args); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Payload is either Mutator or Accessor. Both are supposed to be callable. |
|
140
|
|
|
* |
|
141
|
|
|
* @param callable $payload |
|
142
|
|
|
* @param array $args |
|
143
|
|
|
* @return $this|mixed |
|
144
|
|
|
*/ |
|
145
|
51 |
|
protected function executePayload($payload, $args) |
|
146
|
|
|
{ |
|
147
|
51 |
|
array_unshift($args, $this->wrapped); |
|
148
|
|
|
|
|
149
|
51 |
|
if ($payload instanceof Mutator) { |
|
150
|
|
|
/** @var $payload callable */ |
|
151
|
31 |
|
$this->wrapped = call_user_func_array($payload, $args); |
|
152
|
|
|
|
|
153
|
31 |
|
return $this; |
|
154
|
|
|
} else { |
|
155
|
49 |
|
return call_user_func_array($payload, $args); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|