-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Callback.php
168 lines (140 loc) · 5 KB
/
Callback.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of the zenstruck/callback package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zenstruck;
use Zenstruck\Callback\Argument;
use Zenstruck\Callback\Exception\UnresolveableArgument;
use Zenstruck\Callback\Parameter;
/**
* @author Kevin Bond <[email protected]>
*/
final class Callback implements \Countable
{
/** @var \ReflectionFunction */
private $function;
/** @var Argument[] */
private $arguments;
private function __construct(\ReflectionFunction $function)
{
$this->function = $function;
}
public function __toString(): string
{
if ($this->function->isClosure()) {
return "(closure) {$this->function->getFileName()}:{$this->function->getStartLine()}";
}
return "(function) {$this->function->name}()";
}
/**
* @param callable|\ReflectionFunction $value
*/
public static function createFor($value): self
{
if (\is_callable($value)) {
$value = new \ReflectionFunction(
$value instanceof \Closure || \is_string($value) ? $value : \Closure::fromCallable($value)
);
}
if (!$value instanceof \ReflectionFunction) {
throw new \InvalidArgumentException('$value must be callable.');
}
return new self($value);
}
/**
* Invoke the callable with the passed arguments. Arguments of type
* Zenstruck\Callback\Parameter are resolved before invoking.
*
* @param mixed|Parameter ...$arguments
*
* @return mixed
*
* @throws \ArgumentCountError If there is a argument count mismatch
* @throws UnresolveableArgument If the argument cannot be resolved
*/
public function invoke(...$arguments)
{
$functionArgs = $this->arguments();
foreach ($arguments as $key => $parameter) {
if (!$parameter instanceof Parameter) {
continue;
}
if (!\array_key_exists($key, $functionArgs)) {
if (!$parameter->isOptional()) {
throw new \ArgumentCountError(\sprintf('No argument %d for callable. Expected type: "%s".', $key + 1, $parameter->type()));
}
$arguments[$key] = null;
continue;
}
try {
$arguments[$key] = $parameter->resolve($functionArgs[$key]);
} catch (UnresolveableArgument $e) {
throw new UnresolveableArgument(\sprintf('Unable to resolve argument %d for callback. Expected type: "%s". (%s)', $key + 1, $parameter->type(), $this), $e);
}
}
try {
return $this->function->invoke(...$arguments);
} catch (\ArgumentCountError $e) {
throw new \ArgumentCountError(\sprintf('Too few arguments passed to "%s". Expected %d, got %s.', $this, $this->function->getNumberOfRequiredParameters(), \count($arguments)), 0, $e);
}
}
/**
* Invoke the callable using the passed Parameter to resolve all callable
* arguments.
*
* @param int $min Enforce a minimum number of arguments the callable must have
*
* @return mixed
*
* @throws \ArgumentCountError If the number of arguments is less than $min
* @throws UnresolveableArgument If the argument cannot be resolved
*/
public function invokeAll(Parameter $parameter, int $min = 0)
{
if (\count($this) < $min) {
throw new \ArgumentCountError("{$min} argument(s) of type \"{$parameter->type()}\" required ({$this}).");
}
$arguments = $this->arguments();
foreach ($arguments as $key => $argument) {
try {
$arguments[$key] = $parameter->resolve($argument);
} catch (UnresolveableArgument $e) {
throw new UnresolveableArgument(\sprintf('Unable to resolve argument %d for callback. Expected type: "%s". (%s)', $key + 1, $parameter->type(), $this), $e);
}
}
return $this->function->invoke(...$arguments);
}
/**
* @return Argument[]
*/
public function arguments(): array
{
if (isset($this->arguments)) {
return $this->arguments;
}
return $this->arguments = \array_map(
static fn(\ReflectionParameter $parameter) => new Argument($parameter),
$this->function->getParameters()
);
}
public function argument(int $index): Argument
{
if (!isset(($arguments = $this->arguments())[$index])) {
throw new \OutOfRangeException(\sprintf('Argument %d does not exist for %s.', $index + 1, $this));
}
return $arguments[$index];
}
public function function(): \ReflectionFunction
{
return $this->function;
}
public function count(): int
{
return $this->function->getNumberOfParameters();
}
}