Skip to content

Commit

Permalink
fix use parse_http_query from laravel-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Jan 13, 2024
1 parent 36b266a commit 604b94d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Http/ApplyFiltersToQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function applyFilters(Builder $query, array $filters): Builder
$isScope => $this->applyFilterAsScope($query, $relationship, $scopeName, $operator, $value, $condition),
default => null,
};
}, $query, $filterAttribute, $filterValues);
}, $query, $filterAttribute, (array) $filterValues);
}

return $query;
Expand All @@ -71,9 +71,9 @@ protected function applyFilters(Builder $query, array $filters): Builder
* Wrap query if relationship found applying its operator and conditional to the filtered attribute.
*
* @param callable(\Illuminate\Database\Eloquent\Builder, string|null, string, string, string, string): mixed $callback
* @param array<string>|string $filterValues
* @param array<string> $filterValues
*/
protected function wrapIfRelatedQuery(callable $callback, Builder $query, string $filterAttribute, array|string $filterValues): void
protected function wrapIfRelatedQuery(callable $callback, Builder $query, string $filterAttribute, array $filterValues): void
{
$systemPreferredOperator = $this->allowed[$filterAttribute]['operator'];

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->queryParameters()->get('sort', '')));
$sortsSourceArr = array_filter(explode(',', $this->queryParameters()->get('sort', [''])[0]));
$sortsArr = [];

while ($sort = array_pop($sortsSourceArr)) {
Expand Down
13 changes: 5 additions & 8 deletions src/Http/RequestQueryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\HeaderUtils;

use function OpenSoutheners\LaravelHelpers\Utils\parse_http_query;

/**
* @template T of \Illuminate\Database\Eloquent\Model
Expand Down Expand Up @@ -57,13 +58,9 @@ public function setQuery($query): self
public function queryParameters(): Collection
{
if (! $this->queryParameters) {
$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(
parse_http_query($this->request->server('QUERY_STRING'))
);
}

return $this->queryParameters;
Expand Down
1 change: 1 addition & 0 deletions tests/Http/JsonApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function setUp(): void
parent::setUp();

$this->generateTestData();
$this->withoutExceptionHandling();
}

public function testFilteringByNonAllowedAttributeWillGetEverything()
Expand Down

0 comments on commit 604b94d

Please sign in to comment.