Added ability to init SearchQuery props on construction
Pull Request
What does this PR do?
This lets you init the SearchQuery props when the object is constructed. I think the PHP library is the only one that doesn't yet let you do this or use a simple array instead. I created this because I was using the search() method but needed to switch to multiSearch(), but found it works completely differently, this speeds up using it.
Previous way:
$data = (new SearchQuery())
->setIndexUid('books')
->setQuery('butler')
->setSort(['author:desc']);
Additional way:
$data = new SearchQuery([
'indexUid' => 'books',
'q' => 'butler',
'sort' => ['author:desc'],
]);
Note 1: My change currently does not pass the test due to the use of variable property access. However I think it's safe because all the props are simple and typed. If you think that's ok, I can have phpstan ignore that line.
------ -----------------------------------------------------------------------
Line src/Contracts/SearchQuery.php
------ -----------------------------------------------------------------------
33 Variable property access on $this(Meilisearch\Contracts\SearchQuery).
------ -----------------------------------------------------------------------
Note 2: The initial code I based this on from main currently does not pass all the phpstan tests:
------ ------------------------------------------------------------------------------
Line tests/Endpoints/DocumentsTest.php
------ ------------------------------------------------------------------------------
324 Call to an undefined method
PHPUnit\Framework\MockObject\Rule\InvokedCount::numberOfInvocations().
805 Call to an undefined method
PHPUnit\Framework\MockObject\Rule\InvokedAtLeastOnce::numberOfInvocations().
------ ------------------------------------------------------------------------------
PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?
I added in the linter ignore for the Variable property access line.
Is it really worth? With array you don't have autocompletion, because the structure is undefined
I added in the linter ignore for the Variable property access line.
I'd document the structure instead of ignoring
I've updated the PR so all the properties are in the constructor. With PHP8+ you can specify the args using named arguments along with array spreading to get the same effect of setting all those properties quickly and easily using an array.
@Enchiridion do you still plan to work on this PR? 😊
Closing this for lack of activity. Feel free to re-open of course