Skip to content

Commit dde3a5b

Browse files
author
vidy
committed
Explain filter context
1 parent 8ee5b7a commit dde3a5b

4 files changed

+24
-13
lines changed

README.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Laravel Blade Filters
1010
- [Internal filters](#internal-filters)
1111
- [Testing](#testing)
1212

13-
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.
13+
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.
1414

15-
Because this library is almost refactored, this package renamed as `videni/blade-filters`, but the namespace still keeps it is.
15+
Because this library is almost refactored and rewritten, this package renamed as `videni/blade-filters`, but the namespace still keeps it is.
1616

1717
## Installation
1818

@@ -26,7 +26,7 @@ composer require "videni/blade-filters": "^1.0"
2626
{{ 'a wonderful place' | slug:separator='_', language='en' }}
2727
```
2828

29-
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.
29+
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.
3030

3131

3232
## Pass variables to filter arguments
@@ -61,19 +61,28 @@ $registry
6161

6262
Uncommonly, your filter may be context aware, let's assume a context like this:
6363

64-
A filter named `cdn_url` which generated url for an asset.
64+
A filter named `cdn_url` which generates url for an asset.
6565
```php
6666
cdn_url('assets/carousel.css');
6767
```
6868
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`.
6969

70+
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`.
71+
72+
```
73+
public function getFilterContext(): string
74+
{
75+
return sprintf('%s::', $this->class);
76+
}
77+
```
7078
## Internal filters
7179

72-
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.
80+
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.
7381

7482

7583
## Testing
7684

7785
```
78-
phpunit
86+
composer install
87+
./vendor/bin/phpunit
7988
```

src/BladeFiltersCompiler.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected function compileFilters($value)
5050
$first = array_shift($filters);
5151

5252
$wrapped = sprintf(
53-
$this->getContainer($first['name']).'%s(%s,%s)',
53+
$this->getFilterContext($first['name']).'%s(%s,%s)',
5454
$first['name'],
5555
$prefiltered,
5656
$this->stringifyArguments($first['name'], $first['arguments'])
@@ -61,7 +61,7 @@ protected function compileFilters($value)
6161
$arguments = $filter['arguments'];
6262

6363
$wrapped = sprintf(
64-
$this->getContainer($filterName).'%s(%s,%s)',
64+
$this->getFilterContext($filterName).'%s(%s,%s)',
6565
$filterName,
6666
$wrapped,
6767
$this->stringifyArguments($filterName, $arguments)
@@ -82,11 +82,11 @@ private function stringifyArguments(string $filterName, array $arguments): strin
8282
throw new MissingBladeFilterException(sprintf('Blade filter %s not exists', $filterName));
8383
}
8484

85-
private function getContainer(string $filterName): string
85+
private function getFilterContext(string $filterName): string
8686
{
8787
foreach($this->registry->all() as $filterProvider) {
8888
if ($filterProvider->hasFilter($filterName)) {
89-
return $filterProvider->getContainer();
89+
return $filterProvider->getFilterContext();
9090
}
9191
}
9292

src/FilterProvider/FilterProviderInterface.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public function hasFilter(string $filterName): bool;
2222
public function processFilterArguments(string $filterName, array $filterArguments): string;
2323

2424
/**
25-
* The stringified container for the filter to run
25+
* A string represents filter context where a filter to run, which
26+
* could be a full qualified class name or a variable in Blade view,
27+
* it must have method access operator(->, ::) suffix.
2628
*
2729
* @return string
2830
*/
29-
public function getContainer(): string;
31+
public function getFilterContext(): string;
3032
}

src/FilterProvider/StaticMacroableFilterProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function processFilterArguments(string $filterName, array $filterArgument
4747
/**
4848
* {@inheritDoc}
4949
*/
50-
public function getContainer(): string
50+
public function getFilterContext(): string
5151
{
5252
return sprintf('%s::', $this->class);
5353
}

0 commit comments

Comments
 (0)