1 | <?php |
||
5 | class Functions |
||
6 | { |
||
7 | /** |
||
8 | * Creates a function that memoizes the result of the $payload |
||
9 | * |
||
10 | * @param callable $payload |
||
11 | * |
||
12 | * @return \Closure |
||
13 | */ |
||
14 | 1 | public static function memoize($payload) |
|
30 | |||
31 | /** |
||
32 | * Creates function that simply returns given argument |
||
33 | * |
||
34 | * @return \Closure |
||
35 | */ |
||
36 | 2 | public static function nop() |
|
42 | |||
43 | /** |
||
44 | * Creates a version of the function that can only be called one time. |
||
45 | * |
||
46 | * Repeated calls to the modified function will have no effect, returning |
||
47 | * the value from the original call. Useful for initialization functions, |
||
48 | * instead of having to set a boolean flag and then check it later. |
||
49 | * |
||
50 | * @param callable $function |
||
51 | * @return \Closure |
||
52 | */ |
||
53 | 1 | public static function once($function) |
|
68 | |||
69 | /** |
||
70 | * Provides a constant value that is only used for partial(). |
||
71 | * |
||
72 | * @return \Closure |
||
73 | */ |
||
74 | 1 | public static function p() |
|
75 | { |
||
76 | 1 | static $function; |
|
77 | 1 | if (!$function) { |
|
78 | $function = function () { |
||
79 | 1 | return 'placeholder for partial'; |
|
80 | 1 | }; |
|
81 | 1 | } |
|
82 | |||
83 | 1 | return $function; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Partially apply a function by filling in any number of its arguments. |
||
88 | * |
||
89 | * You may pass p() in your list of arguments to specify an argument that |
||
90 | * should not be pre-filled, but left open to supply at call-time. |
||
91 | * |
||
92 | * @param callable $function |
||
93 | * @param mixed $arg |
||
94 | * @param mixed ... |
||
95 | * @return \Closure |
||
96 | */ |
||
97 | 1 | public static function partial($function, $arg) |
|
122 | |||
123 | /** |
||
124 | * Returns the composition of a list of functions. |
||
125 | * |
||
126 | * Each function consumes the return value of the function that follows. |
||
127 | * In math terms, composing the functions f(), g(), and h() produces f(g(h())). |
||
128 | * |
||
129 | * @param callable $function |
||
130 | * @param callable ... |
||
131 | * @return \Closure |
||
132 | */ |
||
133 | 1 | public static function compose($function) |
|
145 | |||
146 | /** |
||
147 | * Wraps the first function inside of the wrapper function, passing it as the first argument. |
||
148 | * |
||
149 | * This allows the wrapper to execute code before and after the function runs, |
||
150 | * adjust the arguments, and execute it conditionally. |
||
151 | * |
||
152 | * @param callable $function |
||
153 | * @param callable $wrapper |
||
154 | * @return \Closure |
||
155 | */ |
||
156 | 1 | public static function wrap($function, $wrapper) |
|
164 | |||
165 | /** |
||
166 | * Creates and returns a new, throttled version of the passed function. |
||
167 | * |
||
168 | * When invoked repeatedly, will only actually call the original function at |
||
169 | * most once per every wait milliseconds. Useful for rate-limiting events that |
||
170 | * occur faster than you can keep up with. |
||
171 | * |
||
172 | * By default, throttle will execute the function as soon as you call it |
||
173 | * for the first time, and, if you call it again any number of times during |
||
174 | * the wait period, as soon as that period is over. If you'd like to disable |
||
175 | * the leading-edge call, pass `leading => false`. |
||
176 | * |
||
177 | * NOTE: Does not support the `trailing` option because there is no timeout |
||
178 | * functionality in PHP (without using threads). |
||
179 | * |
||
180 | * NOTE: No arguments are passed to the function on the leading edge call! |
||
181 | * |
||
182 | * @param callable $function |
||
183 | * @param integer $wait |
||
184 | * @param array $options |
||
185 | * @return \Closure |
||
186 | */ |
||
187 | 1 | public static function throttle($function, $wait, array $options = []) |
|
188 | { |
||
189 | $options += [ |
||
190 | 1 | 'leading' => true, |
|
191 | ]; // @codeCoverageIgnore |
||
192 | |||
193 | 1 | $previous = 0; |
|
194 | |||
195 | $callback = function () use ($function, $wait, &$previous) { |
||
196 | 1 | $now = floor(microtime(true) * 1000); // convert float to integer |
|
197 | |||
198 | 1 | if (($wait - ($now - $previous)) <= 0) { |
|
199 | 1 | $args = func_get_args(); |
|
200 | 1 | call_user_func_array($function, $args); |
|
201 | 1 | $previous = $now; |
|
202 | 1 | } |
|
203 | 1 | }; |
|
204 | |||
205 | 1 | if ($options['leading'] !== false) { |
|
206 | 1 | $callback(); |
|
207 | 1 | } |
|
208 | |||
209 | 1 | return $callback; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Creates a version of the function that will only be run after first being called count times. |
||
214 | * |
||
215 | * Useful for grouping asynchronous responses, where you want to be sure that |
||
216 | * all the async calls have finished, before proceeding. |
||
217 | * |
||
218 | * @param integer $count |
||
219 | * @param callable $function |
||
220 | * @return \Closure |
||
221 | */ |
||
222 | 1 | public static function after($count, $function) |
|
232 | |||
233 | /** |
||
234 | * Creates a version of the function that can be called no more than count times. |
||
235 | * |
||
236 | * The result of the last function call is memoized and returned when count has been reached. |
||
237 | * |
||
238 | * @param integer $count |
||
239 | * @param callable $function |
||
240 | * @return \Closure |
||
241 | */ |
||
242 | 1 | public static function before($count, $function) |
|
255 | } |
||
256 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.