Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit a5b9452

Browse files
committed
Refactored sorting
- Added more options for sorting - Sort by field - Sort by nested - Sort by filtered nested
1 parent 285eb06 commit a5b9452

File tree

4 files changed

+650
-35
lines changed

4 files changed

+650
-35
lines changed

Query/Query.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ class Query implements HttpTransportable
7878
private $itemsPromoted = [];
7979

8080
/**
81-
* @var array
81+
* @var SortBy
8282
*
83-
* Sort
83+
* Sort by
8484
*/
85-
private $sort;
85+
private $sortBy;
8686

8787
/**
8888
* @var Aggregation[]
@@ -168,7 +168,7 @@ class Query implements HttpTransportable
168168
*/
169169
private function __construct(string $queryText)
170170
{
171-
$this->sortBy(SortBy::SCORE);
171+
$this->sortBy = SortBy::create();
172172
$this->filters['_query'] = Filter::create(
173173
'',
174174
[$queryText],
@@ -649,28 +649,21 @@ public function getFilterFields(): array
649649
/**
650650
* Sort by.
651651
*
652-
* @param array $sort
652+
* @param SortBy $sortBy
653653
*
654654
* @return Query
655655
*/
656-
public function sortBy(array $sort): self
656+
public function sortBy(SortBy $sortBy): self
657657
{
658-
if (isset($sort['_geo_distance'])) {
658+
if ($sortBy->isSortedByGeoDistance()) {
659659
if (!$this->coordinate instanceof Coordinate) {
660660
throw new QueryBuildException('In order to be able to sort by coordinates, you need to create a Query by using Query::createLocated() instead of Query::create()');
661661
}
662-
$sort['_geo_distance']['coordinate'] = $this
663-
->coordinate
664-
->toArray();
665-
}
666662

667-
foreach ($sort as $field => $direction) {
668-
if (!is_array($direction)) {
669-
$sort[$field] = ['order' => $direction];
670-
}
663+
$sortBy->setCoordinate($this->coordinate);
671664
}
672665

673-
$this->sort = $sort;
666+
$this->sortBy = $sortBy;
674667

675668
return $this;
676669
}
@@ -885,11 +878,11 @@ public function getFilterByField(string $fieldName): ? Filter
885878
/**
886879
* Get sort by.
887880
*
888-
* @return array
881+
* @return SortBy
889882
*/
890-
public function getSortBy(): array
883+
public function getSortBy(): SortBy
891884
{
892-
return $this->sort;
885+
return $this->sortBy;
893886
}
894887

895888
/**
@@ -1218,9 +1211,9 @@ public function toArray(): array
12181211
'aggregations' => array_map(function (Aggregation $aggregation) {
12191212
return $aggregation->toArray();
12201213
}, $this->aggregations),
1221-
'sort' => SortBy::SCORE === $this->sort
1222-
? null
1223-
: $this->sort,
1214+
'sort' => $this->sortBy instanceof SortBy
1215+
? $this->sortBy->toArray()
1216+
: null,
12241217
'page' => self::DEFAULT_PAGE === $this->page
12251218
? null
12261219
: $this->page,
@@ -1281,7 +1274,7 @@ public static function createFromArray(array $array): self
12811274
return Aggregation::createFromArray($aggregation);
12821275
}, $array['aggregations'] ?? []);
12831276

1284-
$query->sort = $array['sort'] ?? SortBy::SCORE;
1277+
$query->sortBy = SortBy::createFromArray($array['sort'] ?? []);
12851278
$query->filters = array_merge(
12861279
$query->filters,
12871280
array_map(function (array $filter) {

0 commit comments

Comments
 (0)