Skip to content

Commit 2379e4f

Browse files
committed
Add ability to determine if the search request has sorts
1 parent 8855538 commit 2379e4f

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/Search/SearchRequest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public function sort(array $sort): self
7676
return $this->body('sort', $sort);
7777
}
7878

79+
/**
80+
* Determine if the request has sort clauses.
81+
*/
82+
public function hasSort(): bool
83+
{
84+
return ! empty($this->request['body']['sort'] ?? []);
85+
}
86+
7987
/**
8088
* Set fields to return with optional formatting.
8189
*

tests/Unit/Search/SearchRequestTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@
7575
], $request->toArray());
7676
});
7777

78+
test('sort clauses can be checked', function () {
79+
$request = new SearchRequest;
80+
81+
expect($request->hasSort())->toBeFalse();
82+
83+
$request->sort([]);
84+
85+
expect($request->hasSort())->toBeFalse();
86+
87+
$request->sort([
88+
['title' => 'asc'],
89+
]);
90+
91+
expect($request->hasSort())->toBeTrue();
92+
});
93+
7894
test('array casting with query and search after', function () {
7995
$request = new SearchRequest([
8096
'match_all' => new stdClass,

0 commit comments

Comments
 (0)