-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sort from the request query param
- Loading branch information
Showing
5 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Lelastico\Constants; | ||
|
||
final class SortDirections | ||
{ | ||
public const ASC = 'asc'; | ||
public const DESC = 'desc'; | ||
|
||
public static function getAll(): array | ||
{ | ||
return [ | ||
self::ASC, | ||
self::DESC, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Lelastico\Search\Query\Traits; | ||
|
||
use Erichard\ElasticQueryBuilder\QueryBuilder; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
use Lelastico\Constants\SortDirections; | ||
|
||
/** | ||
* Adds sortable behavior to the query builder from the request "sort" query parameter. | ||
*/ | ||
trait HasSorting | ||
{ | ||
/** | ||
* Allowed fields for sorting. | ||
* | ||
* Key is the name of the field in the query. | ||
* Value is the name of the field in the index. | ||
* | ||
* [ | ||
* 'goals' => 'goals_count' | ||
* 'minutes' => 'played_minutes' | ||
* ] | ||
* | ||
* @return array | ||
*/ | ||
abstract public function allowedSortFields(): array; | ||
|
||
protected function addSort(QueryBuilder $query, Request $request): void | ||
{ | ||
if ($request->missing('sort')) { | ||
return; | ||
} | ||
|
||
$request->validate($this->getValidationRules()); | ||
|
||
foreach ($request->get('sort') as $sort) { | ||
$exploded = explode(':', $sort); | ||
|
||
$field = $this->allowedSortFields()[$exploded[0]]; | ||
$direction = $exploded[1] ?? SortDirections::ASC; | ||
|
||
$query->addSort($field, $direction); | ||
} | ||
} | ||
|
||
private function getSortFields(): array | ||
{ | ||
$values = []; | ||
|
||
foreach (array_keys($this->allowedSortFields()) as $field) { | ||
$values[] = $field; | ||
foreach (SortDirections::getAll() as $direction) { | ||
$values[] = "{$field}:{$direction}"; | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
private function getValidationRules(): array | ||
{ | ||
return [ | ||
'sort' => ['required', 'array'], | ||
'sort.*' => ['required', 'string', Rule::in($this->getSortFields())], | ||
]; | ||
} | ||
} |