Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.
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
54 changes: 50 additions & 4 deletions Result/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class Result implements HttpTransportable
*/
private $subresults = [];

/**
* @var array
*/
private $metadata;

/**
* Result constructor.
*
Expand Down Expand Up @@ -114,7 +119,8 @@ public static function create(
? Aggregations $aggregations,
array $suggestions,
array $items,
? string $autocomplete = null
? string $autocomplete = null,
array $metadata = []
): self {
$result = new self(
$query,
Expand All @@ -123,9 +129,10 @@ public static function create(
);

$result->aggregations = $aggregations;
$result->suggestions = $suggestions;
$result->suggestions = array_combine($suggestions, $suggestions);
$result->items = $items;
$result->autocomplete = $autocomplete;
$result->metadata = $metadata;

return $result;
}
Expand Down Expand Up @@ -382,6 +389,43 @@ public function getSubresults(): array
return $this->subresults;
}

/**
* Set metadata.
*
* @param string $name
* @param mixed $value
*
* @return Query
*/
public function setMetadataValue(
string $name,
$value
): self {
$this->metadata[$name] = $value;

return $this;
}

/**
* Get metadata.
*
* @return array
*/
public function getMetadata(): array
{
return $this->metadata;
}

/**
* Get metadata value.
*
* @return mixed|null
*/
public function getMetadataValue(string $name)
{
return $this->metadata[$name] ?? null;
}

/**
* To array.
*
Expand All @@ -399,13 +443,14 @@ public function toArray(): array
'aggregations' => $this->aggregations instanceof Aggregations
? $this->aggregations->toArray()
: null,
'suggests' => array_keys($this->suggestions),
'suggests' => array_values($this->suggestions),
'autocomplete' => '' === $this->autocomplete
? null
: $this->autocomplete,
'subresults' => array_map(function (Result $result) {
return $result->toArray();
}, $this->subresults),
'metadata' => $this->metadata,
], function ($element) {
return
!(
Expand Down Expand Up @@ -437,7 +482,8 @@ public static function createFromArray(array $array): self
array_map(function (array $item) {
return Item::createFromArray($item);
}, $array['items'] ?? []),
$array['autocomplete'] ?? null
$array['autocomplete'] ?? null,
$array['metadata'] ?? []
);

$result->queryUUID = $array['query_uuid'] ?? '';
Expand Down
12 changes: 12 additions & 0 deletions Tests/Result/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,17 @@ public function testSuggest()
$this->assertEquals(['str1', 'str2'], $result->toArray()['suggests']);
$this->assertEquals(['str1', 'str2'], Result::createFromArray($result->toArray())->getSuggestions());
$this->assertEquals(['str1', 'str2'], Result::createFromArray($result->toArray())->getSuggestions());

$this->assertEquals('sugg1', Result::create(
Query::createMatchAll()->identifyWith('sugg1'), 1, 1, null, [
'sugg1',
], []
)->getSuggestions()[0]);

$this->assertEquals('sugg1', Result::create(
Query::createMatchAll()->identifyWith('sugg1'), 1, 1, null, [
'sugg1',
], []
)->toArray()['suggests'][0]);
}
}