Skip to content

Commit 2c2d1ff

Browse files
author
Nicolas Oelgart
committed
Add support for classes as user functions
1 parent 8e5fc77 commit 2c2d1ff

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/nicoSWD/Rules/Parser.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace nicoSWD\Rules;
1111

1212
use Closure;
13+
use InvalidArgumentException;
14+
use nicoSWD\Rules\Core\CallableUserFunction;
1315
use nicoSWD\Rules\Tokens\BaseToken;
1416

1517
class Parser
@@ -256,6 +258,26 @@ protected function assertSyntaxSeemsOkay()
256258
}
257259
}
258260

261+
public function registerFunctionClass(string $className)
262+
{
263+
/** @var CallableUserFunction $class */
264+
$class = new $className();
265+
266+
if (!$class instanceof CallableUserFunction) {
267+
throw new InvalidArgumentException(
268+
sprintf(
269+
"%s must be an instance of %s",
270+
get_class($className),
271+
CallableUserFunction::class
272+
)
273+
);
274+
}
275+
276+
$this->registerFunction($class->getName(), function () use ($class) {
277+
return $class->call(...func_get_args());
278+
});
279+
}
280+
259281
public function registerFunction(string $name, Closure $callback)
260282
{
261283
$this->userDefinedFunctions[$name] = $callback;

0 commit comments

Comments
 (0)