diff --git a/Result/Result.php b/Result/Result.php index ff9ca18..09175be 100644 --- a/Result/Result.php +++ b/Result/Result.php @@ -74,6 +74,11 @@ class Result implements HttpTransportable */ private $subresults = []; + /** + * @var array + */ + private $metadata; + /** * Result constructor. * @@ -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, @@ -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; } @@ -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. * @@ -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 !( @@ -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'] ?? ''; diff --git a/Tests/Result/ResultTest.php b/Tests/Result/ResultTest.php index b1307c9..bfb4069 100644 --- a/Tests/Result/ResultTest.php +++ b/Tests/Result/ResultTest.php @@ -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]); } }