Skip to content

Commit b4b460d

Browse files
committed
Add ability to sort multiple
1 parent df6fade commit b4b460d

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/RequestParser.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,27 @@ private function parseFilter(array $filterQuery = []): array
133133
return $filters;
134134
}
135135

136-
private function parseSorter(?string $sortQuery): ?SortStruct
136+
private function parseSorter(?string $sortQuery): ?array
137137
{
138138
if(is_null($sortQuery)) {
139-
return null;
139+
return [];
140140
}
141141

142+
$sortStructs = [];
143+
142144
$fieldPattern = "/^\-?([a-zA-z\_]+)$/";
143-
if(preg_match($fieldPattern, $sortQuery, $match)) {
144-
$fieldName = $match[1];
145-
$direction = $sortQuery[0] == "-" ? "DESC" : "ASC";
145+
$splitedSortQuery = explode(",", $sortQuery);
146+
147+
foreach ($splitedSortQuery as $singleSortQuery) {
148+
if(preg_match($fieldPattern, $singleSortQuery, $match)) {
149+
$fieldName = $match[1];
150+
$direction = $singleSortQuery[0] == "-" ? "DESC" : "ASC";
146151

147-
return new SortStruct($fieldName, $direction);
152+
$sortStructs[] = new SortStruct($fieldName, $direction);
153+
}
148154
}
149155

150-
return null;
156+
return $sortStructs;
151157
}
152158

153159
private function getModelFromNamespaces(string $modelName, array $modelNamespaces)
@@ -174,13 +180,17 @@ private function applyFilter(Builder $builder, array $filters): Builder
174180
return $builder;
175181
}
176182

177-
private function applySorter(Builder $builder, ?SortStruct $sorter)
183+
private function applySorter(Builder $builder, array $sorter)
178184
{
179-
if(is_null($sorter)) {
185+
if(empty($sorter)) {
180186
return $builder;
181187
}
182188

183-
return $builder->orderBy($sorter->fieldName, $sorter->direction);
189+
foreach ($sorter as $sort) {
190+
$builder = $builder->orderBy($sort->fieldName, $sort->direction);
191+
}
192+
193+
return $builder;
184194
}
185195

186196
private function createModel(string $baseModelName)

src/Struct/ModelBuilderStruct.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ModelBuilderStruct {
1717
*/
1818
public $sorter;
1919

20-
public function __construct(string $baseModelName, array $filters, ?SortStruct $sorter) {
20+
public function __construct(string $baseModelName, array $filters, array $sorter) {
2121
$this->baseModelName = $baseModelName;
2222
$this->filters = $filters;
2323
$this->sorter = $sorter;

tests/RequestParserTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@ function testSortBy()
8787
$this->assertEquals("desc", strtolower($builder->getQuery()->orders[0]['direction']));
8888
}
8989

90+
function testSortByMultiple()
91+
{
92+
$uri = 'some_model';
93+
$controllerClass = MockModelController::class;
94+
$query = new ParameterBag([
95+
"sort" => "-name,id",
96+
]);
97+
$requestParserOptions = [
98+
'model_namespaces' => [
99+
'LIQRGV\QueryFilter\Mocks',
100+
]
101+
];
102+
103+
$request = $this->createControllerRequest($uri, $controllerClass, $query, $requestParserOptions);
104+
105+
$requestParser = new RequestParser($request);
106+
$builder = $requestParser->getBuilder();
107+
108+
$query = $builder->getQuery();
109+
$this->assertEquals("mock_models", $query->from);
110+
$this->assertEquals("name", $builder->getQuery()->orders[0]['column']);
111+
$this->assertEquals("desc", strtolower($builder->getQuery()->orders[0]['direction']));
112+
$this->assertEquals("id", $builder->getQuery()->orders[1]['column']);
113+
$this->assertEquals("asc", strtolower($builder->getQuery()->orders[1]['direction']));
114+
}
115+
90116
function testSortByWithInvalidField()
91117
{
92118
$uri = 'some_model';

0 commit comments

Comments
 (0)