Skip to content

Commit 3d52fbc

Browse files
committed
Fixed suggestions when Result was transformed to array
1 parent 8fefd03 commit 3d52fbc

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

Result/Result.php

+50-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class Result implements HttpTransportable
7474
*/
7575
private $subresults = [];
7676

77+
/**
78+
* @var array
79+
*/
80+
private $metadata;
81+
7782
/**
7883
* Result constructor.
7984
*
@@ -114,7 +119,8 @@ public static function create(
114119
? Aggregations $aggregations,
115120
array $suggestions,
116121
array $items,
117-
? string $autocomplete = null
122+
? string $autocomplete = null,
123+
array $metadata = []
118124
): self {
119125
$result = new self(
120126
$query,
@@ -123,9 +129,10 @@ public static function create(
123129
);
124130

125131
$result->aggregations = $aggregations;
126-
$result->suggestions = $suggestions;
132+
$result->suggestions = array_combine($suggestions, $suggestions);
127133
$result->items = $items;
128134
$result->autocomplete = $autocomplete;
135+
$result->metadata = $metadata;
129136

130137
return $result;
131138
}
@@ -382,6 +389,43 @@ public function getSubresults(): array
382389
return $this->subresults;
383390
}
384391

392+
/**
393+
* Set metadata.
394+
*
395+
* @param string $name
396+
* @param mixed $value
397+
*
398+
* @return Query
399+
*/
400+
public function setMetadataValue(
401+
string $name,
402+
$value
403+
): self {
404+
$this->metadata[$name] = $value;
405+
406+
return $this;
407+
}
408+
409+
/**
410+
* Get metadata.
411+
*
412+
* @return array
413+
*/
414+
public function getMetadata(): array
415+
{
416+
return $this->metadata;
417+
}
418+
419+
/**
420+
* Get metadata value.
421+
*
422+
* @return mixed|null
423+
*/
424+
public function getMetadataValue(string $name)
425+
{
426+
return $this->metadata[$name] ?? null;
427+
}
428+
385429
/**
386430
* To array.
387431
*
@@ -399,13 +443,14 @@ public function toArray(): array
399443
'aggregations' => $this->aggregations instanceof Aggregations
400444
? $this->aggregations->toArray()
401445
: null,
402-
'suggests' => array_keys($this->suggestions),
446+
'suggests' => array_values($this->suggestions),
403447
'autocomplete' => '' === $this->autocomplete
404448
? null
405449
: $this->autocomplete,
406450
'subresults' => array_map(function (Result $result) {
407451
return $result->toArray();
408452
}, $this->subresults),
453+
'metadata' => $this->metadata,
409454
], function ($element) {
410455
return
411456
!(
@@ -437,7 +482,8 @@ public static function createFromArray(array $array): self
437482
array_map(function (array $item) {
438483
return Item::createFromArray($item);
439484
}, $array['items'] ?? []),
440-
$array['autocomplete'] ?? null
485+
$array['autocomplete'] ?? null,
486+
$array['metadata'] ?? []
441487
);
442488

443489
$result->queryUUID = $array['query_uuid'] ?? '';

Tests/Result/ResultTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,17 @@ public function testSuggest()
263263
$this->assertEquals(['str1', 'str2'], $result->toArray()['suggests']);
264264
$this->assertEquals(['str1', 'str2'], Result::createFromArray($result->toArray())->getSuggestions());
265265
$this->assertEquals(['str1', 'str2'], Result::createFromArray($result->toArray())->getSuggestions());
266+
267+
$this->assertEquals('sugg1', Result::create(
268+
Query::createMatchAll()->identifyWith('sugg1'), 1, 1, null, [
269+
'sugg1',
270+
], []
271+
)->getSuggestions()[0]);
272+
273+
$this->assertEquals('sugg1', Result::create(
274+
Query::createMatchAll()->identifyWith('sugg1'), 1, 1, null, [
275+
'sugg1',
276+
], []
277+
)->toArray()['suggests'][0]);
266278
}
267279
}

0 commit comments

Comments
 (0)