Skip to content

Commit

Permalink
Explain filter context
Browse files Browse the repository at this point in the history
  • Loading branch information
vidy committed Jun 8, 2022
1 parent 8ee5b7a commit dde3a5b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
```
8 changes: 4 additions & 4 deletions src/BladeFiltersCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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)
Expand All @@ -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();
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/FilterProvider/FilterProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/FilterProvider/StaticMacroableFilterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit dde3a5b

Please sign in to comment.