Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to can I register a new function? #6

Open
dmouse opened this issue Jan 18, 2018 · 3 comments
Open

How to can I register a new function? #6

dmouse opened this issue Jan 18, 2018 · 3 comments
Labels

Comments

@dmouse
Copy link

dmouse commented Jan 18, 2018

I'm trying to add a new function to use each function in my custom code

@nicoSWD
Copy link
Owner

nicoSWD commented Jan 18, 2018

Can you show me an example on how this would look like in your code?

What are you planning to do with this?

@jalite1991
Copy link

jalite1991 commented Jan 31, 2022

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(),
];

@nicoSWD
Copy link
Owner

nicoSWD commented Jan 31, 2022

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!

@nicoSWD nicoSWD pinned this issue Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants