Skip to content

Commit

Permalink
fix RequestQueryObject::queryParameters method
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Sep 19, 2023
1 parent 1001b60 commit 365166f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
30 changes: 1 addition & 29 deletions src/Http/Concerns/AllowsFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,7 @@ trait AllowsFilters
*/
public function filters(): array
{
$filters = [];

$this->queryParameters()->each(function ($param) use (&$filters) {
$filterQueryParam = HeaderUtils::parseQuery($param);

if (! is_array(head($filterQueryParam))) {
return;
}

$filterQueryParamAttribute = head(array_keys($filterQueryParam));

if ($filterQueryParamAttribute !== 'filter') {
return;
}

$filterQueryParam = head($filterQueryParam);
$filterQueryParamAttribute = head(array_keys($filterQueryParam));
$filterQueryParamValue = head(array_values($filterQueryParam));

if (! isset($filters[$filterQueryParamAttribute])) {
$filters[$filterQueryParamAttribute] = [$filterQueryParamValue];

return;
}

$filters[$filterQueryParamAttribute][] = $filterQueryParamValue;
});

return $filters;
return $this->queryParameters()->get('filter', []);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Concerns/AllowsSorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait AllowsSorts
*/
public function sorts(): array
{
$sortsSourceArr = array_filter(explode(',', $this->request->get('sort', '')));
$sortsSourceArr = array_filter(explode(',', $this->queryParameters()->get('sort', '')));
$sortsArr = [];

while ($sort = array_pop($sortsSourceArr)) {
Expand Down
31 changes: 28 additions & 3 deletions src/Http/RequestQueryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,38 @@ public function setQuery($query): self
public function queryParameters(): Collection
{
if (! $this->queryParameters) {
$this->queryParameters = Collection::make(
$queryParameters = array_filter(
array_map(
[HeaderUtils::class, 'parseQuery'],
explode('&', $this->request->server('QUERY_STRING', ''))
)
)->groupBy(fn ($item, $key) => head(array_keys($item)), true)
->map(fn (Collection $collection) => $collection->flatten(1)->all());
);

$this->queryParameters = Collection::make([]);

foreach ($queryParameters as $value) {
$parameterKey = array_key_first($value);

if (isset($this->queryParameters[$parameterKey]) && is_array($value[$parameterKey])) {
$this->queryParameters[$parameterKey] = array_merge_recursive(
$value[$parameterKey],
$this->queryParameters[$parameterKey] ?? []
);

continue;
}

if (isset($this->queryParameters[$parameterKey]) && is_string($value[$parameterKey])) {
$this->queryParameters[$parameterKey] = implode(',', array_merge(
explode(',', $value[$parameterKey]),
explode(',', $this->queryParameters[$parameterKey])
));

continue;
}

$this->queryParameters[$parameterKey] = $value[$parameterKey];
}
}

return $this->queryParameters;
Expand Down

0 comments on commit 365166f

Please sign in to comment.