KnpLabs / KnpTimeBundle

Provides helpers for time manipulation
http://knplabs.com
MIT License
604 stars 105 forks source link

Filter ago or time_diff cannot be called with apply_filter #206

Closed rrenteria-dev closed 11 months ago

rrenteria-dev commented 11 months ago

Hello!

I've just updated to 2.2 since 1.20 and i noticed that i cant call the filters with the apply_filter Results in this error:

An exception has been thrown during the rendering of a template ("call_user_func(): Argument #1 ($callback) must be a valid callback, non-static method Knp\Bundle\TimeBundle\DateTimeFormatter::formatDiff() cannot be called statically").

Steps to reproduce:

// test.html.twig

{% set date = date() %} // or any date from controller, db, etc
{{ date|ago }} //works
{{ date|time_diff }} //works

{{date|apply_filter('time_diff')}} //doesn't work
{{date|apply_filter('ago')}} //doesn't work

Im required to apply_filter because 'ago' is being set dynamically. Sure, i can just put an if statement and call the filter directly, but im guessing this is a compatibility error.

Any thoughts? Thanks!

rrenteria-dev commented 11 months ago

Disregard this! Im sorry, i totally forgot that the appy_filter filter is a custom one.

public function applyFilter($env, $value, $filterName, ...$arguments):mixed {
        $twigFilter = $env->getFilter($filterName);
        if (!$twigFilter) {
            return $value;
        }

        return call_user_func($twigFilter->getCallable(), $value, ...$arguments);
    }