diff --git a/src/Contracts/SearchQuery.php b/src/Contracts/SearchQuery.php index d577a134..1c214049 100644 --- a/src/Contracts/SearchQuery.php +++ b/src/Contracts/SearchQuery.php @@ -26,6 +26,35 @@ class SearchQuery private ?int $hitsPerPage; private ?int $page; + // @phpstan-ignore-next-line + public function __construct( + string $indexUid = null, + string $q = null, + array $filter = null, + array $attributesToRetrieve = null, + array $attributesToCrop = null, + int $cropLength = null, + array $attributesToHighlight = null, + string $cropMarker = null, + string $highlightPreTag = null, + string $highlightPostTag = null, + array $facets = null, + bool $showMatchesPosition = null, + array $sort = null, + string $matchingStrategy = null, + int $offset = null, + int $limit = null, + int $hitsPerPage = null, + int $page = null, + ) { + foreach (get_defined_vars() as $name => $value) { + if (!\is_null($value) && property_exists($this, $name)) { + // @phpstan-ignore-next-line + $this->$name = $value; + } + } + } + public function setQuery(string $q): SearchQuery { $this->q = $q; diff --git a/tests/Endpoints/MultiSearchTest.php b/tests/Endpoints/MultiSearchTest.php index 58f84292..d4760777 100644 --- a/tests/Endpoints/MultiSearchTest.php +++ b/tests/Endpoints/MultiSearchTest.php @@ -42,6 +42,22 @@ public function testSearchQueryData(): void ], $data->toArray()); } + public function testSearchQueryDataInitArgs(): void + { + $data = new SearchQuery(...[ + 'indexUid' => $this->booksIndex->getUid(), + 'q' => 'butler', + 'sort' => ['author:desc'], + 'page' => null, + ]); + + $this->assertEquals([ + 'indexUid' => $this->booksIndex->getUid(), + 'q' => 'butler', + 'sort' => ['author:desc'], + ], $data->toArray()); + } + public function testMultiSearch(): void { $response = $this->client->multiSearch([