Skip to content

Commit 45374da

Browse files
committed
Sort by changes a little bit the format to be more comprehensive
Now the model has these plain fields - type (field, distance, nested, score, random, function) - field - order - mode - function - coordinate Updated as well phpunit version
1 parent 8347ecd commit 45374da

File tree

6 files changed

+200
-95
lines changed

6 files changed

+200
-95
lines changed

Query/SortBy.php

+95-47
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ class SortBy implements HttpTransportable
3030
*
3131
* Sort by score
3232
*/
33-
const SCORE = ['_score' => ['order' => self::DESC]];
33+
const SCORE = [
34+
'type' => self::TYPE_SCORE,
35+
];
3436

3537
/**
3638
* @var array
3739
*
3840
* Sort random
3941
*/
40-
const RANDOM = ['random' => true];
42+
const RANDOM = [
43+
'type' => self::TYPE_RANDOM,
44+
];
4145

4246
/**
4347
* @var array
@@ -51,69 +55,102 @@ class SortBy implements HttpTransportable
5155
*
5256
* Sort by id ASC
5357
*/
54-
const ID_ASC = ['uuid.id' => ['order' => self::ASC]];
58+
const ID_ASC = [
59+
'field' => 'uuid.id',
60+
'order' => self::ASC,
61+
];
5562

5663
/**
5764
* @var array
5865
*
5966
* Sort by id DESC
6067
*/
61-
const ID_DESC = ['uuid.id' => ['order' => self::DESC]];
68+
const ID_DESC = [
69+
'field' => 'uuid.id',
70+
'order' => self::DESC,
71+
];
6272

6373
/**
6474
* @var array
6575
*
6676
* Sort by type ASC
6777
*/
68-
const TYPE_ASC = ['uuid.type' => ['order' => self::ASC]];
78+
const TYPE_ASC = [
79+
'field' => 'uuid.type',
80+
'order' => self::ASC,
81+
];
6982

7083
/**
7184
* @var array
7285
*
7386
* Sort by type DESC
7487
*/
75-
const TYPE_DESC = ['uuid.type' => ['order' => self::DESC]];
88+
const TYPE_DESC = [
89+
'field' => 'uuid.type',
90+
'order' => self::DESC,
91+
];
7692

7793
/**
7894
* @var array
7995
*
8096
* Sort by location ASC using KM
8197
*/
82-
const LOCATION_KM_ASC = ['_geo_distance' => ['order' => self::ASC, 'unit' => 'km']];
98+
const LOCATION_KM_ASC = [
99+
'type' => self::TYPE_DISTANCE,
100+
'unit' => 'km',
101+
];
83102

84103
/**
85104
* @var array
86105
*
87106
* Sort by location ASC using Miles
88107
*/
89-
const LOCATION_MI_ASC = ['_geo_distance' => ['order' => self::ASC, 'unit' => 'mi']];
108+
const LOCATION_MI_ASC = [
109+
'type' => self::TYPE_DISTANCE,
110+
'unit' => 'mi',
111+
];
112+
90113
/**
91-
* @var array
114+
* @var string
92115
*
93-
* Sort by id ASC
116+
* Type field
94117
*/
95-
const OCCURRED_ON_ASC = ['indexed_metadata.occurred_on' => ['order' => self::ASC]];
118+
const TYPE_FIELD = 'field';
96119

97120
/**
98-
* @var array
121+
* @var string
99122
*
100-
* Sort by id DESC
123+
* Type score
101124
*/
102-
const OCCURRED_ON_DESC = ['indexed_metadata.occurred_on' => ['order' => self::DESC]];
125+
const TYPE_SCORE = 'score';
103126

104127
/**
105-
* @var int
128+
* @var string
106129
*
107-
* Type field
130+
* Type random
108131
*/
109-
const TYPE_FIELD = 1;
132+
const TYPE_RANDOM = 'random';
110133

111134
/**
112-
* @var int
135+
* @var string
136+
*
137+
* Type distance
138+
*/
139+
const TYPE_DISTANCE = 'distance';
140+
141+
/**
142+
* @var string
113143
*
114144
* Type nested
115145
*/
116-
const TYPE_NESTED = 2;
146+
const TYPE_NESTED = 'nested';
147+
148+
/**
149+
* @var string
150+
*
151+
* Type function
152+
*/
153+
const TYPE_FUNCTION = 'function';
117154

118155
/**
119156
* @var string
@@ -250,9 +287,8 @@ public function byFieldValue(
250287
): SortBy {
251288
$this->sortsBy[] = [
252289
'type' => self::TYPE_FIELD,
253-
Item::getPathByField($field) => [
254-
'order' => $order,
255-
],
290+
'field' => Item::getPathByField($field),
291+
'order' => $order,
256292
];
257293

258294
return $this;
@@ -275,9 +311,8 @@ public function byNestedField(
275311
$this->sortsBy[] = [
276312
'type' => self::TYPE_NESTED,
277313
'mode' => $mode,
278-
"indexed_metadata.$field" => [
279-
'order' => $order,
280-
],
314+
'field' => "indexed_metadata.$field",
315+
'order' => $order,
281316
];
282317

283318
return $this;
@@ -307,9 +342,29 @@ public function byNestedFieldAndFilter(
307342
'type' => self::TYPE_NESTED,
308343
'filter' => $filter,
309344
'mode' => $mode,
310-
"indexed_metadata.$field" => [
311-
'order' => $order,
312-
],
345+
'field' => "indexed_metadata.$field",
346+
'order' => $order,
347+
];
348+
349+
return $this;
350+
}
351+
352+
/**
353+
* Sort by function.
354+
*
355+
* @param string $function
356+
* @param string $order
357+
*
358+
* @return SortBy
359+
*/
360+
public function byFunction(
361+
string $function,
362+
string $order
363+
): SortBy {
364+
$this->sortsBy[] = [
365+
'type' => self::TYPE_FUNCTION,
366+
'function' => $function,
367+
'order' => $order,
313368
];
314369

315370
return $this;
@@ -323,7 +378,7 @@ public function byNestedFieldAndFilter(
323378
public function isSortedByGeoDistance(): bool
324379
{
325380
foreach ($this->sortsBy as $sortBy) {
326-
if (isset($sortBy['_geo_distance'])) {
381+
if (self::TYPE_DISTANCE === $sortBy['type']) {
327382
return true;
328383
}
329384
}
@@ -341,8 +396,8 @@ public function isSortedByGeoDistance(): bool
341396
public function setCoordinate(Coordinate $coordinate)
342397
{
343398
$this->sortsBy = array_map(function (array $sortBy) use ($coordinate) {
344-
if (isset($sortBy['_geo_distance'])) {
345-
$sortBy['_geo_distance']['coordinate'] = $coordinate;
399+
if (self::TYPE_DISTANCE === $sortBy['type']) {
400+
$sortBy['coordinate'] = $coordinate;
346401
}
347402

348403
return $sortBy;
@@ -359,13 +414,6 @@ public function setCoordinate(Coordinate $coordinate)
359414
public function toArray(): array
360415
{
361416
return array_map(function (array $sortBy) {
362-
if (
363-
isset($sortBy['type']) &&
364-
self::TYPE_FIELD == $sortBy['type']
365-
) {
366-
unset($sortBy['type']);
367-
}
368-
369417
if (
370418
isset($sortBy['filter']) &&
371419
($sortBy['filter'] instanceof Filter)
@@ -374,10 +422,10 @@ public function toArray(): array
374422
}
375423

376424
if (
377-
isset($sortBy['_geo_distance']) &&
378-
isset($sortBy['_geo_distance']['coordinate']) &&
379-
($sortBy['_geo_distance']['coordinate'] instanceof Coordinate)) {
380-
$sortBy['_geo_distance']['coordinate'] = $sortBy['_geo_distance']['coordinate']->toArray();
425+
isset($sortBy['coordinate']) &&
426+
$sortBy['coordinate'] instanceof Coordinate
427+
) {
428+
$sortBy['coordinate'] = $sortBy['coordinate']->toArray();
381429
}
382430

383431
return $sortBy;
@@ -409,10 +457,10 @@ public static function createFromArray(array $array)
409457
}
410458

411459
if (
412-
isset($element['_geo_distance']) &&
413-
isset($element['_geo_distance']['coordinate']) &&
414-
is_array($element['_geo_distance']['coordinate'])) {
415-
$element['_geo_distance']['coordinate'] = Coordinate::createFromArray($element['_geo_distance']['coordinate']);
460+
isset($element['coordinate']) &&
461+
is_array($element['coordinate'])
462+
) {
463+
$element['coordinate'] = Coordinate::createFromArray($element['coordinate']);
416464
}
417465

418466
return $element;
@@ -429,7 +477,7 @@ public static function createFromArray(array $array)
429477
public function hasRandomSort()
430478
{
431479
foreach ($this->sortsBy as $sortBy) {
432-
if (self::RANDOM === $sortBy) {
480+
if (self::TYPE_RANDOM === $sortBy['type']) {
433481
return true;
434482
}
435483
}

Tests/Model/CoordinateTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public function testConstructFromArrayWithEmptyArray()
6868
Coordinate::createFromArray([]);
6969
$this->fail('Coordinate should not be built with empty array');
7070
} catch (InvalidFormatException $e) {
71-
// OK
71+
// Silent pass
72+
$this->assertTrue(true);
7273
}
7374
}
7475

@@ -83,7 +84,8 @@ public function testConstructFromArrayWithoutLatitude()
8384
]);
8485
$this->fail('Coordinate should not be built without a latitude');
8586
} catch (InvalidFormatException $e) {
86-
// OK
87+
// Silent pass
88+
$this->assertTrue(true);
8789
}
8890
}
8991

@@ -98,7 +100,8 @@ public function testConstructFromArrayWithoutLongitude()
98100
]);
99101
$this->fail('Coordinate should not be built without a longitude');
100102
} catch (InvalidFormatException $e) {
101-
// OK
103+
// Silent pass
104+
$this->assertTrue(true);
102105
}
103106
}
104107

Tests/Query/FilterTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function testWrongValuesFormat()
133133
$this->fail('InvalidFormatException should be thrown');
134134
} catch (InvalidFormatException $exception) {
135135
// Silent pass
136+
$this->assertTrue(true);
136137
}
137138
}
138139
}

0 commit comments

Comments
 (0)