Skip to content

Commit 11a6dd8

Browse files
author
Olek Kaim
committed
Change logic toCollect method
1 parent 306e9f9 commit 11a6dd8

File tree

2 files changed

+8
-27
lines changed

2 files changed

+8
-27
lines changed

src/Dto/SearchResponseDto.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,13 @@ public function toArray(): array
4747
];
4848
}
4949

50-
public function toCollect(bool $asArray = false): Collection
50+
public function toCollect(): Collection
5151
{
52-
$hitsCollection = $this->getResult()->getHits();
52+
$hits = array_map(
53+
fn(SearchHitDto $searchHitDto) => $searchHitDto->getSource(),
54+
$this->getResult()->getHits()
55+
);
5356

54-
if ($asArray) {
55-
$hits = array_map(
56-
fn(SearchHitDto $searchHitDto) => $searchHitDto->toArray(),
57-
$hitsCollection
58-
);
59-
60-
return Collection::make($hits);
61-
}
62-
63-
return Collection::make($hitsCollection);
57+
return Collection::make($hits);
6458
}
6559
}

tests/Integration/ClientTest.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -678,21 +678,8 @@ public function testSearchResponseCanBeConvertedToCollection(): void
678678

679679
$this->assertInstanceOf(Collection::class, $result->toCollect());
680680

681-
/** @var SearchHitDto $hit */
682-
foreach ($result->toCollect() as $hit) {
683-
$this->assertSame('hello', $hit->getIndex());
684-
$this->assertSame('183865906814918156', $hit->getId());
685-
$this->assertSame(1.0, $hit->getScore());
686-
$this->assertSame(['hello' => 'world'], $hit->getSource());
687-
}
688-
689-
$this->assertInstanceOf(Collection::class, $result->toCollect(asArray: true));
690-
691-
foreach ($result->toCollect(asArray: true) as $hit) {
692-
$this->assertSame('hello', $hit['index']);
693-
$this->assertSame('183865906814918156', $hit['id']);
694-
$this->assertSame(1.0, $hit['score']);
695-
$this->assertSame(['hello' => 'world'], $hit['source']);
681+
foreach ($result->toCollect() as $source) {
682+
$this->assertSame(['hello' => 'world'], $source);
696683
}
697684
}
698685
}

0 commit comments

Comments
 (0)