Skip to content

Commit

Permalink
pass filter arguments as array
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed May 27, 2024
1 parent c8ad369 commit e7bf21c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 130 deletions.
2 changes: 1 addition & 1 deletion src/Nodes/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function evaluate(RenderContext $context): mixed
foreach ($this->filters as [$filterName, $filterArgs, $filterNamedArgs]) {
$filterArgs = $this->evaluateFilterExpressions($context, $filterArgs ?? []);
$filterNamedArgs = $this->evaluateFilterExpressions($context, $filterNamedArgs ?? []);
$output = $context->applyFilter($filterName, $output, ...$filterArgs, ...$filterNamedArgs);
$output = $context->applyFilter($filterName, $output, [...$filterArgs, ...$filterNamedArgs]);
}

return $output;
Expand Down
4 changes: 2 additions & 2 deletions src/Render/RenderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ public function normalizeValue(mixed $value): mixed
return $value;
}

public function applyFilter(string $filter, mixed $value, mixed ...$args): mixed
public function applyFilter(string $filter, mixed $value, array $args = []): mixed
{
return $this->filterRegistry->invoke($this, $filter, $value, ...$args);
return $this->filterRegistry->invoke($this, $filter, $value, $args);
}

public function getRegister(string $name): mixed
Expand Down
8 changes: 4 additions & 4 deletions src/Support/FilterRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public function register(string $filterClass): static
continue;
}

$this->filters[Str::snake($method->getName())] = function (RenderContext $context, ...$args) use ($filterClass, $method) {
$this->filters[Str::snake($method->getName())] = function (RenderContext $context, mixed $value, array $args) use ($filterClass, $method) {
$filterClassInstance = new $filterClass();

if ($filterClassInstance instanceof IsContextAware) {
$filterClassInstance->setContext($context);
}

return $filterClassInstance->{$method->getName()}(...$args);
return $filterClassInstance->{$method->getName()}($value, ...$args);
};
}

Expand All @@ -47,13 +47,13 @@ public function register(string $filterClass): static
/**
* @throws UndefinedFilterException|UndefinedVariableException
*/
public function invoke(RenderContext $context, string $filterName, mixed $value, mixed ...$args): mixed
public function invoke(RenderContext $context, string $filterName, mixed $value, array $args = []): mixed
{
$filter = $this->filters[$filterName] ?? null;

if ($filter !== null) {
try {
return $filter($context, $value, ...$args);
return $filter($context, $value, $args);
} catch (\TypeError $e) {
if ($value instanceof UndefinedVariable) {
throw new UndefinedVariableException($value->variableName);
Expand Down
Loading

0 comments on commit e7bf21c

Please sign in to comment.