php-rule-parser
php-rule-parser copied to clipboard
How to can I register a new function?
I'm trying to add a new function to use each function in my custom code
Can you show me an example on how this would look like in your code?
What are you planning to do with this?
I was looking to add a modulo function, basically [variable] % 10 == 0.
But I see you allow class to be set as variables.
class User
{
// ...
public function points(): int
{
return 1337;
}
}
$variables = [
'user' => new User(),
];
Hey @jalite1991
you have two options. If you're using Symfony, there's a bundle with native support for custom functions.
The other option is to use a custom class and pass it as variable
<?php
$math = new class
{
public function mod($x, $y)
{
return $x % $y;
}
};
$rule = new Rule('math.mod(13, 3) === 1', ['math' => $math]);
var_dump($rule->isTrue()); // bool(true)
I hope this helps!