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

Cannot use variables where one of the keys has a dash in it. #3

Closed
Fuitad opened this issue Feb 16, 2016 · 4 comments
Closed

Cannot use variables where one of the keys has a dash in it. #3

Fuitad opened this issue Feb 16, 2016 · 4 comments
Labels

Comments

@Fuitad
Copy link

Fuitad commented Feb 16, 2016

See the following test (which fails currently) for an example. Not sure if it's work-as-design or not.

Btw, awesome project!

public function testArrayWithDashInKeyDoesParseCorrectly()
    {
        $this->assertTrue($this->evaluate(
            'foo-bar === [
                "foo", // This is foo
                "bar"  // And this is bar
            ]',
            ['foo-bar' => ['foo', 'bar']]
        ));
    }
@Fuitad
Copy link
Author

Fuitad commented Feb 16, 2016

If that's any help, I just tried the same test case but with an underscore and it does pass the test.

public function testArrayWithUnderscoreInKeyDoesParseCorrectly()
    {
        $this->assertTrue($this->evaluate(
            'foo_bar === [
                "foo", // This is foo
                "bar"  // And this is bar
            ]',
            ['foo_bar' => ['foo', 'bar']]
        ));
    }

@nicoSWD
Copy link
Owner

nicoSWD commented Feb 16, 2016

Hi @Fuitad!

Thanks for your submission. This is actually by design. I tried to keep it as close to Javascript syntax as possible. Plus, if I plan on adding math equations in the future, the dash would be interpreted as minus (foo minus bar). I'm not sure if I'll ever implement this, but it crossed my mind.

However, if you're using the latest version (0.4.0 as of writing this), you can actually redefine tokens in user-land. This would allow you to do what you want.

Eg:

$rule->registerToken(Tokenizer::TOKEN_VARIABLE, '[a-zA-Z_][\w-]*');

I hope this helps!

@cnanney
Copy link

cnanney commented Feb 2, 2024

@nicoSWD Is this no longer possible? Using 0.6.1 and there is no longer public access to the tokenizer, and all properties are private so I can't extend Rule and add a method to access from the container.

@nicoSWD
Copy link
Owner

nicoSWD commented Feb 5, 2024

@cnanney hi! It's still possible, but the approach has changed. It's now preferred to use composition instead of inheritance.

Meaning, you'd have to create your own grammar (akin to this) and construct your own container using said grammar like this.

Rule.php is extendable so you should be able to override the constructor.

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