Skip to content

Commit

Permalink
Process filter arguments in filter provider
Browse files Browse the repository at this point in the history
  • Loading branch information
vidy committed Jun 7, 2022
1 parent 20541eb commit 8ee5b7a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 of `\Illuminate\Support\Str`, it will be simply ignored.


## Pass variables to filter arguments
Expand Down
11 changes: 2 additions & 9 deletions src/BladeFiltersCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,11 @@ private function stringifyArguments(string $filterName, array $arguments): strin
{
foreach($this->registry->all() as $filterProvider) {
if ($filterProvider->hasFilter($filterName)) {
$argumentNames = $filterProvider->getFilterArgumentNames($filterName);
// Remove the first argument, because the first argument is the value being filtered.
array_shift($argumentNames);
// Fill argument values
$argumentNames = array_flip($argumentNames);

$arguments = array_intersect_key($arguments, $argumentNames);

return join(',', empty($arguments)? []: array_values($arguments));
return $filterProvider->processFilterArguments($filterName, $arguments);
}
}

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

private function getContainer(string $filterName): string
Expand Down
9 changes: 4 additions & 5 deletions src/FilterProvider/FilterProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ interface FilterProviderInterface
public function hasFilter(string $filterName): bool;

/**
* Get filter argument names sorted in order
* Only used in blade compile phrase
* Process filter arguments
*
* @param string $filterName
*
* @return array
* @param array $filterArguments
* @return string
*/
public function getFilterArgumentNames(string $filterName): array;
public function processFilterArguments(string $filterName, array $filterArguments): string;

/**
* The stringified container for the filter to run
Expand Down
43 changes: 33 additions & 10 deletions src/FilterProvider/StaticMacroableFilterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,38 @@ public function hasFilter(string $filterName): bool
/**
* {@inheritDoc}
*/
public function getFilterArgumentNames(string $filterName): array
public function processFilterArguments(string $filterName, array $filterArguments): string
{
$argumentNames = $this->getFilterArgumentNames($filterName);

// Remove the first argument, because the first argument is the value being filtered.
array_shift($argumentNames);
// Fill argument values
$argumentNames = array_flip($argumentNames);

$arguments = array_intersect_key($filterArguments, $argumentNames);

return join(',', empty($arguments)? []: array_values($arguments));
}


/**
* {@inheritDoc}
*/
public function getContainer(): string
{
return sprintf('%s::', $this->class);
}

/**
* Get filter argument names sorted in order
* Only used in blade compile phrase
*
* @param string $filterName
*
* @return array
*/
protected function getFilterArgumentNames(string $filterName): array
{
$ref = new \ReflectionClass($this->class);
$method = null;
Expand All @@ -40,19 +71,11 @@ public function getFilterArgumentNames(string $filterName): array
$micros = $ref->getStaticProperties();
$method = new \ReflectionFunction($micros['macros'][$filterName]);
} else {
throw new MissingBladeFilterException(sprintf('Blade filter %s not exists', $filterName));
throw new MissingBladeFilterException(sprintf('Blade filter %s not exists in class %s', $filterName, $this->class));
}

return array_map(function($param) {
return $param->name;
}, $method->getParameters());
}

/**
* {@inheritDoc}
*/
public function getContainer(): string
{
return sprintf('%s::', $this->class);
}
}

0 comments on commit 8ee5b7a

Please sign in to comment.