Skip to content

Added some new features #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ class Query implements HttpTransportable
*/
private $queryOperator = null;

/**
* @var array
*/
private $context = [];

/**
* Construct.
*
Expand Down Expand Up @@ -1535,6 +1540,26 @@ public function getQueryOperator(): ?string
return $this->queryOperator ?? self::QUERY_OPERATOR_OR;
}

/**
* @return array
*/
public function getContext(): array
{
return $this->context;
}

/**
* @param array $context
*
* @return Query
*/
public function setContext(array $context): Query
{
$this->context = $context;

return $this;
}

/**
* To array.
*
Expand Down Expand Up @@ -1612,6 +1637,7 @@ public function toArray(): array
'query_operator' => self::QUERY_OPERATOR_OR !== $this->queryOperator
? $this->queryOperator
: null,
'context' => $this->context,
], function ($element) {
return
!(
Expand Down Expand Up @@ -1695,6 +1721,8 @@ public static function createFromArray(array $array): self
$query->queryOperator = $array['query_operator'];
}

$query->context = $array['context'] ?? [];

return $query;
}
}
5 changes: 4 additions & 1 deletion Query/SortBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,20 @@ public function byNestedFieldAndFilter(
*
* @param string $function
* @param string $order
* @param array $params
*
* @return SortBy
*/
public function byFunction(
string $function,
string $order
string $order,
array $params = []
): SortBy {
$this->sortsBy[] = [
'type' => self::TYPE_FUNCTION,
'function' => $function,
'order' => $order,
'params' => $params,
];

return $this;
Expand Down
28 changes: 28 additions & 0 deletions Result/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class Result implements HttpTransportable
*/
private $metadata;

/**
* @var array
*/
private $context = [];

/**
* Result constructor.
*
Expand Down Expand Up @@ -434,6 +439,26 @@ public function getMetadataValue(string $name)
return $this->metadata[$name] ?? null;
}

/**
* @return array
*/
public function getContext(): array
{
return $this->context;
}

/**
* @param array $context
*
* @return Result
*/
public function setContext(array $context): Result
{
$this->context = $context;

return $this;
}

/**
* To array.
*
Expand All @@ -459,6 +484,7 @@ public function toArray(): array
return $result->toArray();
}, $this->subresults),
'metadata' => $this->metadata,
'context' => $this->context,
], function ($element) {
return
!(
Expand Down Expand Up @@ -501,6 +527,8 @@ public static function createFromArray(array $array): self
}, $array['subresults'] ?? [])
);

$result->context = $array['context'] ?? [];

return $result;
}
}
13 changes: 13 additions & 0 deletions Tests/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,17 @@ public function testForceSizeAndPage()
$this->assertEquals(3, $query->getPage());
$this->assertEquals(10, $query->getFrom());
}

public function testContext()
{
$this->assertEmpty(Query::createMatchAll()->getContext());
$this->assertEmpty((Query::createMatchAll())->toArray()['context'] ?? []);
$query = Query::createMatchAll()->setContext(['context1']);
$queryAsArray = [
'context' => ['context1'],
];
$this->assertEquals($queryAsArray, $query->toArray());
$this->assertEquals(['context1'], $query->getContext());
$this->assertEquals($queryAsArray, Query::createFromArray($query->toArray())->toArray());
}
}
16 changes: 16 additions & 0 deletions Tests/Query/SortByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,20 @@ public function testMultiplesSorts()
HttpHelper::emulateHttpTransport($sortBy)
);
}

public function testByFunction()
{
$sortBy = SortBy::create()->byFunction('function 1', 'asc', ['val1', 'val2']);
$sortByAsArray = [
[
'type' => 'function',
'function' => 'function 1',
'order' => 'asc',
'params' => ['val1', 'val2'],
],
];

$this->assertEquals($sortByAsArray, $sortBy->toArray());
$this->assertEquals($sortByAsArray, SortBy::createFromArray($sortByAsArray)->toArray());
}
}
15 changes: 15 additions & 0 deletions Tests/Result/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,19 @@ public function testSuggest()
$result->setSuggestions(['sugg100']);
$this->assertEquals(['sugg100'], $result->getSuggestions());
}

public function testContext()
{
$this->assertEmpty((new Result(Query::createMatchAll(), 0, 0))->getContext());
$this->assertEmpty((new Result(Query::createMatchAll(), 0, 0))->toArray()['context'] ?? []);
$result = (new Result(Query::createMatchAll(), 0, 0))->setContext(['context1']);
$resultAsArray = [
'total_items' => 0,
'total_hits' => 0,
'context' => ['context1'],
];
$this->assertEquals($resultAsArray, $result->toArray());
$this->assertEquals(['context1'], $result->getContext());
$this->assertEquals(['context' => ['context1']], Query::createFromArray($result->toArray())->toArray());
}
}