-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
ChainableClosure.php
67 lines (58 loc) · 2.2 KB
/
ChainableClosure.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace Pest\Support;
use Closure;
use Pest\Exceptions\ShouldNotHappen;
/**
* @internal
*/
final class ChainableClosure
{
/**
* Calls the given `$closure` when the given condition is true, "bound" to the same object.
*/
public static function boundWhen(Closure $condition, Closure $next): Closure
{
return function () use ($condition, $next): void {
if (! is_object($this)) { // @phpstan-ignore-line
throw ShouldNotHappen::fromMessage('$this not bound to chainable closure.');
}
if (\Pest\Support\Closure::bind($condition, $this, self::class)(...func_get_args())) {
\Pest\Support\Closure::bind($next, $this, self::class)(...func_get_args());
}
};
}
/**
* Calls the given `$closure` and chains the `$next` closure, "bound" to the same object.
*/
public static function bound(Closure $closure, Closure $next): Closure
{
return function () use ($closure, $next): void {
if (! is_object($this)) { // @phpstan-ignore-line
throw ShouldNotHappen::fromMessage('$this not bound to chainable closure.');
}
\Pest\Support\Closure::bind($closure, $this, self::class)(...func_get_args());
\Pest\Support\Closure::bind($next, $this, self::class)(...func_get_args());
};
}
/**
* Calls the given `$closure` and chains the `$next` closure, "unbound" of any object.
*/
public static function unbound(Closure $closure, Closure $next): Closure
{
return function () use ($closure, $next): void {
$closure(...func_get_args());
$next(...func_get_args());
};
}
/**
* Call the given static `$closure` and chains the `$next` closure, "bound" to the same object statically.
*/
public static function boundStatically(Closure $closure, Closure $next): Closure
{
return static function () use ($closure, $next): void {
\Pest\Support\Closure::bind($closure, null, self::class)(...func_get_args());
\Pest\Support\Closure::bind($next, null, self::class)(...func_get_args());
};
}
}