|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
|
5
|
|
|
* @link https://github.com/nicoSWD |
|
6
|
|
|
* @author Nicolas Oelgart <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace nicoSWD\Rule\TokenStream; |
|
9
|
|
|
|
|
10
|
|
|
use nicoSWD\Rule\Grammar\CallableUserFunctionInterface; |
|
11
|
|
|
use nicoSWD\Rule\TokenStream\Token\BaseToken; |
|
12
|
|
|
use nicoSWD\Rule\TokenStream\Token\TokenFactory; |
|
13
|
|
|
|
|
14
|
|
|
final class CallableUserMethod implements CallableUserFunctionInterface |
|
15
|
|
|
{ |
|
16
|
|
|
const MAGIC_METHOD_PREFIX = '__'; |
|
17
|
|
|
|
|
18
|
|
|
/** @var TokenFactory */ |
|
19
|
|
|
private $tokenFactory; |
|
20
|
|
|
/** @var callable */ |
|
21
|
|
|
private $callable; |
|
22
|
|
|
/** @var string[] */ |
|
23
|
|
|
private $methodPrefixes = ['', 'get', 'is', 'get_', 'is_']; |
|
24
|
|
|
|
|
25
|
52 |
|
public function __construct(BaseToken $token, TokenFactory $tokenFactory, string $methodName) |
|
26
|
|
|
{ |
|
27
|
52 |
|
$this->tokenFactory = $tokenFactory; |
|
28
|
52 |
|
$this->callable = $this->getCallable($token, $methodName); |
|
29
|
18 |
|
} |
|
30
|
|
|
|
|
31
|
18 |
|
public function call(?BaseToken ...$param): BaseToken |
|
32
|
|
|
{ |
|
33
|
18 |
|
$callable = $this->callable; |
|
34
|
|
|
|
|
35
|
18 |
|
return $this->tokenFactory->createFromPHPType( |
|
36
|
18 |
|
$callable(...$param) |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
52 |
|
private function getCallable(BaseToken $token, string $methodName): callable |
|
41
|
|
|
{ |
|
42
|
52 |
|
$object = $token->getValue(); |
|
43
|
|
|
|
|
44
|
52 |
|
if (property_exists($object, $methodName)) { |
|
45
|
|
|
return function () use ($object, $methodName) { |
|
46
|
4 |
|
return $object->{$methodName}; |
|
47
|
4 |
|
}; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
48 |
|
$method = $this->findCallableMethod($object, $methodName); |
|
51
|
|
|
|
|
52
|
|
|
return function (?BaseToken ...$params) use ($method) { |
|
53
|
14 |
|
return $method(...$this->getTokenValues($params)); |
|
54
|
14 |
|
}; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
48 |
|
private function findCallableMethod($object, string $methodName): callable |
|
58
|
|
|
{ |
|
59
|
48 |
|
$this->assertNonMagicMethod($methodName); |
|
60
|
|
|
|
|
61
|
18 |
|
$callable = [$object, $methodName]; |
|
62
|
18 |
|
$index = 0; |
|
63
|
|
|
|
|
64
|
|
|
do { |
|
65
|
18 |
|
if (!isset($this->methodPrefixes[$index])) { |
|
66
|
4 |
|
throw new Exception\UndefinedMethodException(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
18 |
|
$callable[1] = $this->methodPrefixes[$index++] . $methodName; |
|
70
|
18 |
|
} while (!is_callable($callable)); |
|
71
|
|
|
|
|
72
|
14 |
|
return $callable; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function getTokenValues(array $params): array |
|
76
|
|
|
{ |
|
77
|
|
|
return array_map(function (BaseToken $token) { return $token->getValue(); }, $params); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
48 |
|
private function assertNonMagicMethod(string $methodName): void |
|
81
|
|
|
{ |
|
82
|
48 |
|
if (substr($methodName, 0, 2) === self::MAGIC_METHOD_PREFIX) { |
|
83
|
30 |
|
throw new Exception\ForbiddenMethodException(); |
|
84
|
|
|
} |
|
85
|
18 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|