Skip to content

Commit 8ee5b7a

Browse files
author
vidy
committed
Process filter arguments in filter provider
1 parent 20541eb commit 8ee5b7a

4 files changed

+40
-25
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -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 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 of `\Illuminate\Support\Str`, it will be simply ignored.
3030

3131

3232
## Pass variables to filter arguments

src/BladeFiltersCompiler.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,11 @@ private function stringifyArguments(string $filterName, array $arguments): strin
7575
{
7676
foreach($this->registry->all() as $filterProvider) {
7777
if ($filterProvider->hasFilter($filterName)) {
78-
$argumentNames = $filterProvider->getFilterArgumentNames($filterName);
79-
// Remove the first argument, because the first argument is the value being filtered.
80-
array_shift($argumentNames);
81-
// Fill argument values
82-
$argumentNames = array_flip($argumentNames);
83-
84-
$arguments = array_intersect_key($arguments, $argumentNames);
85-
86-
return join(',', empty($arguments)? []: array_values($arguments));
78+
return $filterProvider->processFilterArguments($filterName, $arguments);
8779
}
8880
}
8981

82+
throw new MissingBladeFilterException(sprintf('Blade filter %s not exists', $filterName));
9083
}
9184

9285
private function getContainer(string $filterName): string

src/FilterProvider/FilterProviderInterface.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ interface FilterProviderInterface
1313
public function hasFilter(string $filterName): bool;
1414

1515
/**
16-
* Get filter argument names sorted in order
17-
* Only used in blade compile phrase
16+
* Process filter arguments
1817
*
1918
* @param string $filterName
20-
*
21-
* @return array
19+
* @param array $filterArguments
20+
* @return string
2221
*/
23-
public function getFilterArgumentNames(string $filterName): array;
22+
public function processFilterArguments(string $filterName, array $filterArguments): string;
2423

2524
/**
2625
* The stringified container for the filter to run

src/FilterProvider/StaticMacroableFilterProvider.php

+33-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,38 @@ public function hasFilter(string $filterName): bool
2929
/**
3030
* {@inheritDoc}
3131
*/
32-
public function getFilterArgumentNames(string $filterName): array
32+
public function processFilterArguments(string $filterName, array $filterArguments): string
33+
{
34+
$argumentNames = $this->getFilterArgumentNames($filterName);
35+
36+
// Remove the first argument, because the first argument is the value being filtered.
37+
array_shift($argumentNames);
38+
// Fill argument values
39+
$argumentNames = array_flip($argumentNames);
40+
41+
$arguments = array_intersect_key($filterArguments, $argumentNames);
42+
43+
return join(',', empty($arguments)? []: array_values($arguments));
44+
}
45+
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
50+
public function getContainer(): string
51+
{
52+
return sprintf('%s::', $this->class);
53+
}
54+
55+
/**
56+
* Get filter argument names sorted in order
57+
* Only used in blade compile phrase
58+
*
59+
* @param string $filterName
60+
*
61+
* @return array
62+
*/
63+
protected function getFilterArgumentNames(string $filterName): array
3364
{
3465
$ref = new \ReflectionClass($this->class);
3566
$method = null;
@@ -40,19 +71,11 @@ public function getFilterArgumentNames(string $filterName): array
4071
$micros = $ref->getStaticProperties();
4172
$method = new \ReflectionFunction($micros['macros'][$filterName]);
4273
} else {
43-
throw new MissingBladeFilterException(sprintf('Blade filter %s not exists', $filterName));
74+
throw new MissingBladeFilterException(sprintf('Blade filter %s not exists in class %s', $filterName, $this->class));
4475
}
4576

4677
return array_map(function($param) {
4778
return $param->name;
4879
}, $method->getParameters());
4980
}
50-
51-
/**
52-
* {@inheritDoc}
53-
*/
54-
public function getContainer(): string
55-
{
56-
return sprintf('%s::', $this->class);
57-
}
5881
}

0 commit comments

Comments
 (0)