Skip to content

Commit 94439ff

Browse files
committed
Added some new features
- Sort by function accepts params - Query and Result have now a context array
1 parent b391f47 commit 94439ff

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

Query/Query.php

+28
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ class Query implements HttpTransportable
186186
*/
187187
private $queryOperator = null;
188188

189+
/**
190+
* @var array
191+
*/
192+
private $context = [];
193+
189194
/**
190195
* Construct.
191196
*
@@ -1535,6 +1540,26 @@ public function getQueryOperator(): ?string
15351540
return $this->queryOperator ?? self::QUERY_OPERATOR_OR;
15361541
}
15371542

1543+
/**
1544+
* @return array
1545+
*/
1546+
public function getContext(): array
1547+
{
1548+
return $this->context;
1549+
}
1550+
1551+
/**
1552+
* @param array $context
1553+
*
1554+
* @return Query
1555+
*/
1556+
public function setContext(array $context): Query
1557+
{
1558+
$this->context = $context;
1559+
1560+
return $this;
1561+
}
1562+
15381563
/**
15391564
* To array.
15401565
*
@@ -1612,6 +1637,7 @@ public function toArray(): array
16121637
'query_operator' => self::QUERY_OPERATOR_OR !== $this->queryOperator
16131638
? $this->queryOperator
16141639
: null,
1640+
'context' => $this->context,
16151641
], function ($element) {
16161642
return
16171643
!(
@@ -1695,6 +1721,8 @@ public static function createFromArray(array $array): self
16951721
$query->queryOperator = $array['query_operator'];
16961722
}
16971723

1724+
$query->context = $array['context'] ?? [];
1725+
16981726
return $query;
16991727
}
17001728
}

Query/SortBy.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,20 @@ public function byNestedFieldAndFilter(
354354
*
355355
* @param string $function
356356
* @param string $order
357+
* @param array $params
357358
*
358359
* @return SortBy
359360
*/
360361
public function byFunction(
361362
string $function,
362-
string $order
363+
string $order,
364+
array $params = []
363365
): SortBy {
364366
$this->sortsBy[] = [
365367
'type' => self::TYPE_FUNCTION,
366368
'function' => $function,
367369
'order' => $order,
370+
'params' => $params,
368371
];
369372

370373
return $this;

Result/Result.php

+28
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ class Result implements HttpTransportable
7979
*/
8080
private $metadata;
8181

82+
/**
83+
* @var array
84+
*/
85+
private $context = [];
86+
8287
/**
8388
* Result constructor.
8489
*
@@ -434,6 +439,26 @@ public function getMetadataValue(string $name)
434439
return $this->metadata[$name] ?? null;
435440
}
436441

442+
/**
443+
* @return array
444+
*/
445+
public function getContext(): array
446+
{
447+
return $this->context;
448+
}
449+
450+
/**
451+
* @param array $context
452+
*
453+
* @return Result
454+
*/
455+
public function setContext(array $context): Result
456+
{
457+
$this->context = $context;
458+
459+
return $this;
460+
}
461+
437462
/**
438463
* To array.
439464
*
@@ -459,6 +484,7 @@ public function toArray(): array
459484
return $result->toArray();
460485
}, $this->subresults),
461486
'metadata' => $this->metadata,
487+
'context' => $this->context,
462488
], function ($element) {
463489
return
464490
!(
@@ -501,6 +527,8 @@ public static function createFromArray(array $array): self
501527
}, $array['subresults'] ?? [])
502528
);
503529

530+
$result->context = $array['context'] ?? [];
531+
504532
return $result;
505533
}
506534
}

Tests/Query/QueryTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,17 @@ public function testForceSizeAndPage()
385385
$this->assertEquals(3, $query->getPage());
386386
$this->assertEquals(10, $query->getFrom());
387387
}
388+
389+
public function testContext()
390+
{
391+
$this->assertEmpty(Query::createMatchAll()->getContext());
392+
$this->assertEmpty((Query::createMatchAll())->toArray()['context'] ?? []);
393+
$query = Query::createMatchAll()->setContext(['context1']);
394+
$queryAsArray = [
395+
'context' => ['context1'],
396+
];
397+
$this->assertEquals($queryAsArray, $query->toArray());
398+
$this->assertEquals(['context1'], $query->getContext());
399+
$this->assertEquals($queryAsArray, Query::createFromArray($query->toArray())->toArray());
400+
}
388401
}

Tests/Query/SortByTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,20 @@ public function testMultiplesSorts()
387387
HttpHelper::emulateHttpTransport($sortBy)
388388
);
389389
}
390+
391+
public function testByFunction()
392+
{
393+
$sortBy = SortBy::create()->byFunction('function 1', 'asc', ['val1', 'val2']);
394+
$sortByAsArray = [
395+
[
396+
'type' => 'function',
397+
'function' => 'function 1',
398+
'order' => 'asc',
399+
'params' => ['val1', 'val2'],
400+
],
401+
];
402+
403+
$this->assertEquals($sortByAsArray, $sortBy->toArray());
404+
$this->assertEquals($sortByAsArray, SortBy::createFromArray($sortByAsArray)->toArray());
405+
}
390406
}

Tests/Result/ResultTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,19 @@ public function testSuggest()
279279
$result->setSuggestions(['sugg100']);
280280
$this->assertEquals(['sugg100'], $result->getSuggestions());
281281
}
282+
283+
public function testContext()
284+
{
285+
$this->assertEmpty((new Result(Query::createMatchAll(), 0, 0))->getContext());
286+
$this->assertEmpty((new Result(Query::createMatchAll(), 0, 0))->toArray()['context'] ?? []);
287+
$result = (new Result(Query::createMatchAll(), 0, 0))->setContext(['context1']);
288+
$resultAsArray = [
289+
'total_items' => 0,
290+
'total_hits' => 0,
291+
'context' => ['context1'],
292+
];
293+
$this->assertEquals($resultAsArray, $result->toArray());
294+
$this->assertEquals(['context1'], $result->getContext());
295+
$this->assertEquals(['context' => ['context1']], Query::createFromArray($result->toArray())->toArray());
296+
}
282297
}

0 commit comments

Comments
 (0)