diff --git a/README.md b/README.md index a8aecac..209c7d3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/BladeFiltersCompiler.php b/src/BladeFiltersCompiler.php index 3c7d6ca..e65cdb3 100644 --- a/src/BladeFiltersCompiler.php +++ b/src/BladeFiltersCompiler.php @@ -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 diff --git a/src/FilterProvider/FilterProviderInterface.php b/src/FilterProvider/FilterProviderInterface.php index 0d0d769..248fabe 100644 --- a/src/FilterProvider/FilterProviderInterface.php +++ b/src/FilterProvider/FilterProviderInterface.php @@ -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 diff --git a/src/FilterProvider/StaticMacroableFilterProvider.php b/src/FilterProvider/StaticMacroableFilterProvider.php index 8afdf26..5943064 100644 --- a/src/FilterProvider/StaticMacroableFilterProvider.php +++ b/src/FilterProvider/StaticMacroableFilterProvider.php @@ -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; @@ -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); - } }