|
14 | 14 | use nicoSWD\Rule\TokenStream\Token\BaseToken; |
15 | 15 | use nicoSWD\Rule\TokenStream\Token\TokenFactory; |
16 | 16 | use nicoSWD\Rule\Tokenizer\TokenizerInterface; |
| 17 | +use nicoSWD\Rule\TokenStream\Token\TokenObject; |
17 | 18 |
|
18 | 19 | class AST |
19 | 20 | { |
@@ -47,6 +48,10 @@ public function getStream(string $rule): TokenStream |
47 | 48 |
|
48 | 49 | public function getMethod(string $methodName, BaseToken $token): CallableUserFunctionInterface |
49 | 50 | { |
| 51 | + if ($token instanceof TokenObject) { |
| 52 | + return $this->getUserObjectCallable($token, $methodName); |
| 53 | + } |
| 54 | + |
50 | 55 | if (empty($this->methods)) { |
51 | 56 | $this->registerMethods(); |
52 | 57 | } |
@@ -120,4 +125,35 @@ private function registerFunctions() |
120 | 125 | $this->registerFunctionClass($functionName, $className); |
121 | 126 | } |
122 | 127 | } |
| 128 | + |
| 129 | + private function getUserObjectCallable(BaseToken $token, string $methodName): CallableUserFunctionInterface |
| 130 | + { |
| 131 | + return new class ($token, $this->tokenFactory, $methodName) implements CallableUserFunctionInterface |
| 132 | + { |
| 133 | + /** @var BaseToken */ |
| 134 | + private $token; |
| 135 | + /** @var TokenFactory */ |
| 136 | + private $tokenFactory; |
| 137 | + /** @var string */ |
| 138 | + private $methodName; |
| 139 | + |
| 140 | + public function __construct(BaseToken $token, TokenFactory $tokenFactory, string $methodName) |
| 141 | + { |
| 142 | + $this->token = $token; |
| 143 | + $this->tokenFactory = $tokenFactory; |
| 144 | + $this->methodName = $methodName; |
| 145 | + } |
| 146 | + |
| 147 | + public function call(BaseToken $param = null): BaseToken |
| 148 | + { |
| 149 | + $object = [$this->token->getValue(), $this->methodName]; |
| 150 | + |
| 151 | + if (!is_callable($object)) { |
| 152 | + throw new \Exception(); |
| 153 | + } |
| 154 | + |
| 155 | + return $this->tokenFactory->createFromPHPType($object()); |
| 156 | + } |
| 157 | + }; |
| 158 | + } |
123 | 159 | } |
0 commit comments