Skip to content

Commit

Permalink
fixed bugs + added default sort option
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Mar 2, 2022
1 parent 3615c9f commit 6eb7d4a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 17 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to `Query Filter Builder` will be documented in this file

## 1.1.2 - 2022-03-02

- a few bugfixes
- added default sorting option

# 1.1.1 - 2022-02-28

- fixed bug where joins weren't allowed when using custom filters with Laravel.

## 1.1.0 - 2022-02-24

- added FormRequest functionality to ease development in Laravel
Expand Down
8 changes: 6 additions & 2 deletions doc/LARAVEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ class YourFormRequest extends FormRequest {
protected bool $enablePagination = false; /** disable or enable pagination */

protected bool $enableSorting = false; /** disable or enable sorting */
protected array $allowSorting = []; /** fields that are allowed for sorting */
protected array $allowedSorting = []; /** fields that are allowed for sorting */
protected array|string $defaultSort = []; /** default sort, can be a string or an array */
protected int $defaultLimit = 10; /** The default limit */
protected int $maxLimit = 50; /** The maximum allowed limit */
}
```
By default, the package does not allow any fields for sorting.
You have to add the fields you want to allow into the `$allowSorting` property.
You have to add the fields you want to allow into the `$allowedSorting` property.
The format is the same as specified in the
[JSON:API specification: Sorting](https://jsonapi.org/format/1.1/#fetching-sorting),
except it's listed as an array:
Expand All @@ -59,6 +60,9 @@ protected array $allowSorting = [
'-animal' /** descending */
];
```

`defaultSort` works the same way, except it is allowed to be a string.

### Add your own filters
To add your own filters, simply add the following method in your FormRequest.
You can use `filter` and `hasFilter` methods as shortcut to the filter
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private function globalFilters(Filter $filter)
{
$query = DB::query();
$this->filterProxy($filter)->build($query);

if ($query->joins) {

$this->getBuilder()->joins = array_merge(
Expand Down
37 changes: 28 additions & 9 deletions src/Illuminate/Factories/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ private function __construct(private Query $query, private array $parameters)

public static function get(
InputBag $inputBag,
bool $enableSorting,
bool $enablePagination,
int $defaultLimit = null
?bool $enableSorting,
array|string|null $defaultSorting,
?bool $enablePagination,
?int $defaultLimit,
): Query
{
$query = new Query;

$filterFactory = new self($query, $inputBag->all());

if ($enablePagination || config('filter.pagination.auto')) {
if ((null === $enablePagination && config('filter.pagination.auto')) || $enablePagination) {
$filterFactory->parsePagination($defaultLimit);
}

if ($enableSorting || config('filter.sorting.auto')) {
$filterFactory->parseSorting();
if ((null === $enableSorting && config('filter.sorting.auto')) || $enableSorting) {
$filterFactory->parseSorting($defaultSorting);
}

return $query;
Expand Down Expand Up @@ -57,14 +58,19 @@ private function createConfigKey(array $paths): string
return join('.', array_filter($paths));
}

private function parseSorting(): void
private function parseSorting(array|string|null $defaultSorting): void
{
$key = config('filter.sorting.key', 'sort');

$sorting = $this->stringToArray($defaultSorting);

if (Arr::has($this->parameters, $key)) {
$sorts = explode(',', $this->parameters[$key]);
$sorting = $this->stringToArray($this->parameters[$key]);
}

if ($sorting) {

foreach ($sorts as $sort) {
foreach ($sorting as $sort) {

if (str_starts_with($sort, '-')) {
$this->query->desc(ltrim($sort, '-'));
Expand All @@ -76,4 +82,17 @@ private function parseSorting(): void
}
}

private function stringToArray(string|array|null $value): ?array
{
if(is_array($value)) {
return $value;
}

if(null===$value) {
return null;
}

return explode(',', $value);
}

}
7 changes: 4 additions & 3 deletions src/Illuminate/Mixins/FormRequestMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ public function getFilter(): Closure

$filters = FilterFactory::get(
$this->query,
$this->enableSorting ?? false,
$this->enablePagination ?? false,
$this->defaultLimit ?? null
$this->enableSorting ?? null,
$this->defaultSort ?? null,
$this->enablePagination ?? null,
$this->defaultLimit ?? null,
);

if (method_exists($this, 'filters')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function providesEnablePaginationTestcases()
{
return [
'config-disabled-fq-enabled' => [false, true,(new Query())->limit(12)],
'config-enabled-fq-disabled' => [true, false, (new Query())->limit(12)],
'config-enabled-fq-disabled' => [true, false, (new Query())],
'both-disabled' => [false, false, (new Query())],
];
}
Expand Down
44 changes: 43 additions & 1 deletion tests/Unit/Illuminate/Mixins/FormRequestMxin/SortingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function providesEnablePaginationTestcases()
'config-disabled-fq-enabled' =>
[false, true, (new Query())->limit(50)->asc('animal')],
'config-enabled-fq-disabled' =>
[true, false, (new Query())->limit(50)->asc('animal')],
[true, false, (new Query())->limit(50)],
'both-enabled' =>
[true, true, (new Query())->limit(50)->asc('animal')],
'both-disabled' =>
Expand Down Expand Up @@ -150,4 +150,46 @@ public function testShouldThrowValidationExceptionForNotAllowedFields()

$formRequest->getFilter();
}

public function testShouldAllowDefaultSortingInFormRequest()
{
$formRequest = $this->getFormRequest();
$formRequest->enablePagination = false;
$formRequest->defaultSort = 'id';


$this->assertEquals(
(new Query())->asc('id'),
$formRequest->getFilter()
);
}

public function testShouldAllowDefaultSortingAsArrayInFormRequest()
{
$formRequest = $this->getFormRequest();
$formRequest->enablePagination = false;
$formRequest->defaultSort = ['-id'];


$this->assertEquals(
(new Query())->desc('id'),
$formRequest->getFilter()
);
}

public function testShouldNotUseDefaultWhenSortIsGiven()
{
$formRequest = $this->getFormRequest();

$formRequest->enablePagination = false;
$formRequest->allowedSorting = ['animal'];
$formRequest->defaultSort = ['-id'];

$formRequest->query = new InputBag(['sort' => 'animal']);

$this->assertEquals(
(new Query())->asc('animal'),
$formRequest->getFilter()
);
}
}

0 comments on commit 6eb7d4a

Please sign in to comment.