Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to init SearchQuery props on construction #546

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions src/Contracts/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the vars manually then the phpstan won't complain and in future it could be refactored to CPP

if (!\is_null($value) && property_exists($this, $name)) {
// @phpstan-ignore-next-line
$this->$name = $value;
}
}
}

public function setQuery(string $q): SearchQuery
{
$this->q = $q;
Expand Down
16 changes: 16 additions & 0 deletions tests/Endpoints/MultiSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
Loading