From dde3a5b862a6b1942f6ff28489e10ee8be0ec3cf Mon Sep 17 00:00:00 2001 From: vidy Date: Wed, 8 Jun 2022 09:40:09 +0800 Subject: [PATCH] Explain filter context --- README.md | 21 +++++++++++++------ src/BladeFiltersCompiler.php | 8 +++---- .../FilterProviderInterface.php | 6 ++++-- .../StaticMacroableFilterProvider.php | 2 +- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 209c7d3..4bfaa8c 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ Laravel Blade Filters - [Internal filters](#internal-filters) - [Testing](#testing) -Originated from [`conedevelopment/blade-filters`](https://github.com/conedevelopment/blade-filters), but with huge improvements, the original doesn't support named arguments and filter context, which are essential in my case. this library implements a custom lexer and parser to analyze filter syntax. +Originated from [`conedevelopment/blade-filters`](https://github.com/conedevelopment/blade-filters), but with lots of improvements, the original doesn't support named arguments and filter context, which are essential in my case. this library implements a lexer and parser to analyze filter syntax. -Because this library is almost refactored, this package renamed as `videni/blade-filters`, but the namespace still keeps it is. +Because this library is almost refactored and rewritten, this package renamed as `videni/blade-filters`, but the namespace still keeps it is. ## Installation @@ -26,7 +26,7 @@ composer require "videni/blade-filters": "^1.0" {{ 'a wonderful place' | slug:separator='_', language='en' }} ``` -For slug filter which provided by `\Illuminate\Support\Str`, the first argument is the value being filtered, the second argument would be the `separator`, the third would be `language`, if a argument name doesn't not exists in the slug method of `\Illuminate\Support\Str`, it will be simply ignored. +For slug filter which provided by `\Illuminate\Support\Str`, the first argument is the value being filtered, the second argument would be the `separator`, the third would be `language`, if a argument name doesn't not exists in the slug method, it will be simply ignored. ## Pass variables to filter arguments @@ -61,19 +61,28 @@ $registry Uncommonly, your filter may be context aware, let's assume a context like this: -A filter named `cdn_url` which generated url for an asset. +A filter named `cdn_url` which generates url for an asset. ```php cdn_url('assets/carousel.css'); ``` the domain of the CDN will change depending on the context where the filter run, the context itself is not part of the API of our filter, which the user doesn't need to worry about. you can always pass a variable to your filter as an argument following [Pass variables to filter arguments](#pass-variables-to-filter-arguments), however, the variable must be filled by the filter's user(you or someone), this is the difference between `filter context` and `filter argument`. +filter context is a string which could be a full qualified class name or a variable in Blade view, it must have method access operator( ->, :: ) suffix, an example could be the `getFilterContext` method of class `\Pine\BladeFilters\FilterProvider\StaticMacroableFilterProvider`. + +``` + public function getFilterContext(): string + { + return sprintf('%s::', $this->class); + } +``` ## Internal filters -all static methods from `Pine\BladeFilters\BladeFilters` and `\Illuminate\Support\Str` are provided as blade filters, it is quite simple, please check its source code reference. +all static methods from `Pine\BladeFilters\BladeFilters` and `\Illuminate\Support\Str` are provided as blade filters, it is quite simple, please check its source code for reference. ## Testing ``` -phpunit +composer install +./vendor/bin/phpunit ``` diff --git a/src/BladeFiltersCompiler.php b/src/BladeFiltersCompiler.php index e65cdb3..32fc5bf 100644 --- a/src/BladeFiltersCompiler.php +++ b/src/BladeFiltersCompiler.php @@ -50,7 +50,7 @@ protected function compileFilters($value) $first = array_shift($filters); $wrapped = sprintf( - $this->getContainer($first['name']).'%s(%s,%s)', + $this->getFilterContext($first['name']).'%s(%s,%s)', $first['name'], $prefiltered, $this->stringifyArguments($first['name'], $first['arguments']) @@ -61,7 +61,7 @@ protected function compileFilters($value) $arguments = $filter['arguments']; $wrapped = sprintf( - $this->getContainer($filterName).'%s(%s,%s)', + $this->getFilterContext($filterName).'%s(%s,%s)', $filterName, $wrapped, $this->stringifyArguments($filterName, $arguments) @@ -82,11 +82,11 @@ private function stringifyArguments(string $filterName, array $arguments): strin throw new MissingBladeFilterException(sprintf('Blade filter %s not exists', $filterName)); } - private function getContainer(string $filterName): string + private function getFilterContext(string $filterName): string { foreach($this->registry->all() as $filterProvider) { if ($filterProvider->hasFilter($filterName)) { - return $filterProvider->getContainer(); + return $filterProvider->getFilterContext(); } } diff --git a/src/FilterProvider/FilterProviderInterface.php b/src/FilterProvider/FilterProviderInterface.php index 248fabe..cdcc10d 100644 --- a/src/FilterProvider/FilterProviderInterface.php +++ b/src/FilterProvider/FilterProviderInterface.php @@ -22,9 +22,11 @@ public function hasFilter(string $filterName): bool; public function processFilterArguments(string $filterName, array $filterArguments): string; /** - * The stringified container for the filter to run + * A string represents filter context where a filter to run, which + * could be a full qualified class name or a variable in Blade view, + * it must have method access operator(->, ::) suffix. * * @return string */ - public function getContainer(): string; + public function getFilterContext(): string; } diff --git a/src/FilterProvider/StaticMacroableFilterProvider.php b/src/FilterProvider/StaticMacroableFilterProvider.php index 5943064..023d2ab 100644 --- a/src/FilterProvider/StaticMacroableFilterProvider.php +++ b/src/FilterProvider/StaticMacroableFilterProvider.php @@ -47,7 +47,7 @@ public function processFilterArguments(string $filterName, array $filterArgument /** * {@inheritDoc} */ - public function getContainer(): string + public function getFilterContext(): string { return sprintf('%s::', $this->class); }