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

[ResourceBundle] ParameterParser Request Value on expression #9681

Closed
alx-bongard opened this issue Aug 28, 2018 · 1 comment
Closed

[ResourceBundle] ParameterParser Request Value on expression #9681

alx-bongard opened this issue Aug 28, 2018 · 1 comment
Labels
Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.). RFC Discussions about potential changes or new features. Stale Issues and PRs with no recent activity, about to be closed soon.

Comments

@alx-bongard
Copy link

Hi,

i try to implement a custom expression language function with a request value parameter.

final class DateTimeExpressionFunctionProvider implements ExpressionFunctionProviderInterface
{
    /**
     * {@inheritdoc}
     */
    public function getFunctions(): array
    {
        return [
            new ExpressionFunction('datetime', function ($time = null, $timezone = null) {
                return sprintf('(null !== %1$s) ? (null !== %2$s) ? new \DateTime((null !== %1$s ? %1$s : "now"), new \DateTimeZone(%2$s)) : new \DateTime((null !== %1$s ? %1$s : "now")) : null', $time, $timezone);
            }, function ($arguments, $time = null, $timezone = null) {
                if (null !== $timezone) {
                    $timezone = new \DateTimeZone($timezone);
                }

                return null !== $time ? new \DateTime($time, $timezone) : null;
            })
        ];
    }
}

If i use expr:datetime($start) for example, i can call my route with start parameter or not. But if i want to use a custom default start parameter who depends on my route, i use this syntax :

  • expr:datetime($start ?: "+1 days")
  • expr:datetime($start ?: "+3 days")

But if i call my route without start => Unexpected token "punctuation" of value "?" around position 11 for expression datetime( ?: "+3 days").

For solve my problem, i override ParametersParser class and i change function parseRequestValueExpression line 103 by :

if (is_string($variable)) {
    return sprintf('"%s"', $variable);
}

if (is_null($variable)) {
    return 'null';
}

return $variable;

Of course i need to add = null on php expression function parameter, but i think it's more flexible and my problem is solved.

Thanks for your feedback.

@pamil pamil added Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.). RFC Discussions about potential changes or new features. labels Oct 2, 2018
@stale
Copy link

stale bot commented Jan 1, 2019

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale Issues and PRs with no recent activity, about to be closed soon. label Jan 1, 2019
@stale stale bot closed this as completed Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.). RFC Discussions about potential changes or new features. Stale Issues and PRs with no recent activity, about to be closed soon.
Projects
None yet
Development

No branches or pull requests

2 participants