Skip to content

Commit ca5b19b

Browse files
Added ordering to the search criteria (#35)
1 parent c99f9a3 commit ca5b19b

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ $search = \JustBetter\MagentoClient\Query\SearchCriteria::make()
180180
->orWhere('price', '>', 10),
181181
->whereIn('sku', ['123', '456'])
182182
->orWhereNull('name')
183+
->orderBy('sku')
183184
->paginate(1, 50)
184185
->get();
185186
```

src/Query/SearchCriteria.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ class SearchCriteria
1010

1111
public array $wheres = [];
1212

13+
public array $orders = [];
14+
1315
protected int $currentFilterGroup = 0;
1416

1517
protected int $currentFilterIndex = 0;
1618

17-
public function __construct(protected Grammar $grammar)
18-
{
19+
public function __construct(
20+
protected Grammar $grammar
21+
) {
1922
}
2023

2124
public function paginate(int $page, int $pageSize): static
@@ -169,11 +172,27 @@ protected function addWhere(
169172
return $this;
170173
}
171174

175+
public function orderBy(string $field, string $direction = 'ASC'): static
176+
{
177+
$index = count($this->orders) / 2;
178+
179+
$this->orders["searchCriteria[sortOrders][$index][field]"] = $field;
180+
$this->orders["searchCriteria[sortOrders][$index][direction]"] = $direction;
181+
182+
return $this;
183+
}
184+
185+
public function orderByDesc(string $field): static
186+
{
187+
return $this->orderBy($field, 'DESC');
188+
}
189+
172190
public function get(): array
173191
{
174192
return array_merge(
175193
$this->select,
176194
$this->wheres,
195+
$this->orders,
177196
$this->pagination,
178197
);
179198
}

tests/Query/SearchCriteriaTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,43 @@ public function test_it_can_add_orWhereNotNull(): void
260260
'searchCriteria[filter_groups][0][filters][1][condition_type]' => 'notnull',
261261
], $searchCriteria);
262262
}
263+
264+
public function test_it_can_orderBy(): void
265+
{
266+
$searchCriteria = SearchCriteria::make()
267+
->orderBy('sku')
268+
->get();
269+
270+
$this->assertEquals([
271+
'searchCriteria[sortOrders][0][field]' => 'sku',
272+
'searchCriteria[sortOrders][0][direction]' => 'ASC',
273+
], $searchCriteria);
274+
}
275+
276+
public function test_it_can_orderByDesc(): void
277+
{
278+
$searchCriteria = SearchCriteria::make()
279+
->orderByDesc('sku')
280+
->get();
281+
282+
$this->assertEquals([
283+
'searchCriteria[sortOrders][0][field]' => 'sku',
284+
'searchCriteria[sortOrders][0][direction]' => 'DESC',
285+
], $searchCriteria);
286+
}
287+
288+
public function test_it_can_add_multiple_orders(): void
289+
{
290+
$searchCriteria = SearchCriteria::make()
291+
->orderBy('sku')
292+
->orderByDesc('entity_id')
293+
->get();
294+
295+
$this->assertEquals([
296+
'searchCriteria[sortOrders][0][field]' => 'sku',
297+
'searchCriteria[sortOrders][0][direction]' => 'ASC',
298+
'searchCriteria[sortOrders][1][field]' => 'entity_id',
299+
'searchCriteria[sortOrders][1][direction]' => 'DESC',
300+
], $searchCriteria);
301+
}
263302
}

0 commit comments

Comments
 (0)