Skip to content

Commit

Permalink
Added ordering to the search criteria (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonrietdijk committed Feb 5, 2024
1 parent c99f9a3 commit ca5b19b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ $search = \JustBetter\MagentoClient\Query\SearchCriteria::make()
->orWhere('price', '>', 10),
->whereIn('sku', ['123', '456'])
->orWhereNull('name')
->orderBy('sku')
->paginate(1, 50)
->get();
```
Expand Down
23 changes: 21 additions & 2 deletions src/Query/SearchCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ class SearchCriteria

public array $wheres = [];

public array $orders = [];

protected int $currentFilterGroup = 0;

protected int $currentFilterIndex = 0;

public function __construct(protected Grammar $grammar)
{
public function __construct(
protected Grammar $grammar
) {
}

public function paginate(int $page, int $pageSize): static
Expand Down Expand Up @@ -169,11 +172,27 @@ protected function addWhere(
return $this;
}

public function orderBy(string $field, string $direction = 'ASC'): static
{
$index = count($this->orders) / 2;

$this->orders["searchCriteria[sortOrders][$index][field]"] = $field;
$this->orders["searchCriteria[sortOrders][$index][direction]"] = $direction;

return $this;
}

public function orderByDesc(string $field): static
{
return $this->orderBy($field, 'DESC');
}

public function get(): array
{
return array_merge(
$this->select,
$this->wheres,
$this->orders,
$this->pagination,
);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Query/SearchCriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,43 @@ public function test_it_can_add_orWhereNotNull(): void
'searchCriteria[filter_groups][0][filters][1][condition_type]' => 'notnull',
], $searchCriteria);
}

public function test_it_can_orderBy(): void
{
$searchCriteria = SearchCriteria::make()
->orderBy('sku')
->get();

$this->assertEquals([
'searchCriteria[sortOrders][0][field]' => 'sku',
'searchCriteria[sortOrders][0][direction]' => 'ASC',
], $searchCriteria);
}

public function test_it_can_orderByDesc(): void
{
$searchCriteria = SearchCriteria::make()
->orderByDesc('sku')
->get();

$this->assertEquals([
'searchCriteria[sortOrders][0][field]' => 'sku',
'searchCriteria[sortOrders][0][direction]' => 'DESC',
], $searchCriteria);
}

public function test_it_can_add_multiple_orders(): void
{
$searchCriteria = SearchCriteria::make()
->orderBy('sku')
->orderByDesc('entity_id')
->get();

$this->assertEquals([
'searchCriteria[sortOrders][0][field]' => 'sku',
'searchCriteria[sortOrders][0][direction]' => 'ASC',
'searchCriteria[sortOrders][1][field]' => 'entity_id',
'searchCriteria[sortOrders][1][direction]' => 'DESC',
], $searchCriteria);
}
}

0 comments on commit ca5b19b

Please sign in to comment.