Skip to content

Commit

Permalink
fix(jsonapi): add support for pagination array parameter name
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien COURJEAN committed Apr 16, 2024
1 parent b77e7ab commit c2ddc80
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/JsonApi/State/JsonApiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$filterParameter
&& \is_array($filterParameter)
) {
$filters = array_merge($filterParameter, $filters);
$filters = array_merge(['page' => $filterParameter], $filterParameter, $filters);
}

$pageParameter = $queryParameters['page'] ?? null;
Expand Down
17 changes: 16 additions & 1 deletion src/State/Pagination/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,28 @@ private function getGraphQlEnabled(?Operation $operation): bool
return $operation?->getPaginationEnabled() ?? $enabled;
}

private function extractParameter(array $contextFilters, string $parameterName)
{
\preg_match_all("/[\w-]+/", $parameterName, $matches);

foreach($matches[0] as $key){
if (!\array_key_exists($key, $contextFilters)) {
return null;
}
$contextFilters = $contextFilters[$key];
}

return $contextFilters;
}

/**
* Gets the given pagination parameter name from the given context.
*/
private function getParameterFromContext(array $context, string $parameterName, mixed $default = null)
{
$filters = $context['filters'] ?? [];
$filterValue = $this->extractParameter($filters, $parameterName);

return \array_key_exists($parameterName, $filters) ? $filters[$parameterName] : $default;
return $filterValue ?? $default;
}
}

0 comments on commit c2ddc80

Please sign in to comment.