@@ 43-52 (lines=10) @@ | ||
40 | * @param callable $callback The callable to wrap. |
|
41 | * @return \Closure The wrapper. |
|
42 | */ |
|
43 | public static function after(int $times = 1, callable $callback) : \Closure |
|
44 | { |
|
45 | return function(...$args) use ($callback, $times) { |
|
46 | static $count = 0; |
|
47 | ||
48 | if (++$count >= $times) { |
|
49 | return $callback(...$args); |
|
50 | } |
|
51 | }; |
|
52 | } |
|
53 | ||
54 | /** |
|
55 | * Returns a Closure which will return the subsequent given value (argument to this method) on each call. |
|
@@ 169-180 (lines=12) @@ | ||
166 | * @param callable $callback The callable to wrap. |
|
167 | * @return \Closure The wrapper. |
|
168 | */ |
|
169 | public static function only(int $times = 1, callable $callback) : \Closure |
|
170 | { |
|
171 | return function(...$args) use ($callback, $times) { |
|
172 | // Keep track of how many times the Closure was already called. |
|
173 | static $called = 0; |
|
174 | ||
175 | // Invoke the callback when we didn't hit our limit yet. |
|
176 | if ($times >= ++$called) { |
|
177 | return $callback(...$args); |
|
178 | } |
|
179 | }; |
|
180 | } |
|
181 | ||
182 | /** |
|
183 | * Creates a Closure that, when called, invokes the wrapped callable with any additional partial arguments |