Skip to content
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"php": "^7.4 || ^8.0",
"ext-json": "*",
"php-http/discovery": "^1.7",
"psr/http-client": "^1.0"
"psr/http-client": "^1.0",
"symfony/polyfill-php81": "^1.33"
},
"autoload": {
"psr-4": {
Expand Down
42 changes: 19 additions & 23 deletions src/Contracts/DocumentsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class DocumentsQuery
*/
private ?array $ids = null;

/**
* @var list<non-empty-string>|null
*/
private ?array $sort = null;

/**
* @param non-negative-int $offset
*
Expand Down Expand Up @@ -117,46 +122,37 @@ public function hasFilter(): bool
return null !== $this->filter;
}

/**
* @param list<non-empty-string> $sort
*/
public function setSort(array $sort): self
{
$this->sort = $sort;

return $this;
}

/**
* @return array{
* offset?: non-negative-int,
* limit?: non-negative-int,
* fields?: non-empty-list<string>|non-empty-string,
* filter?: list<non-empty-string|list<non-empty-string>>,
* retrieveVectors?: bool,
* ids?: string
* ids?: string,
* sort?: non-empty-list<string>,
* }
*/
public function toArray(): array
{
return array_filter([
'offset' => $this->offset,
'limit' => $this->limit,
'fields' => $this->getFields(),
'fields' => $this->fields,
'filter' => $this->filter,
'retrieveVectors' => $this->retrieveVectors,
'ids' => ($this->ids ?? []) !== [] ? implode(',', $this->ids) : null,
'sort' => $this->sort,
], static function ($item) { return null !== $item; });
}

/**
* Prepares fields for request
* Fix for 1.2 document/fetch.
*
* @see https://github.com/meilisearch/meilisearch-php/issues/522
*
* @return array|string|null
*/
private function getFields()
{
if (null === $this->fields) {
return null;
}

if (null !== $this->filter) {
return $this->fields;
}

return implode(',', $this->fields);
}
}
3 changes: 3 additions & 0 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ private function buildQueryString(array $queryParams = []): string
if (\is_bool($value)) {
$queryParams[$key] = $value ? 'true' : 'false';
}
if (\is_array($value) && \array_is_list($value)) {
$queryParams[$key] = implode(',', $value);
}
}

return \count($queryParams) > 0 ? '?'.http_build_query($queryParams) : '';
Expand Down
2 changes: 1 addition & 1 deletion tests/Contracts/DocumentsQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testSetFields(): void
{
$data = (new DocumentsQuery())->setLimit(10)->setFields(['abc', 'xyz']);

self::assertSame(['limit' => 10, 'fields' => 'abc,xyz'], $data->toArray());
self::assertSame(['limit' => 10, 'fields' => ['abc', 'xyz']], $data->toArray());
}

public function testSetLimit(): void
Expand Down
44 changes: 31 additions & 13 deletions tests/Endpoints/DocumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,30 +666,48 @@ public function testGetDocumentsWithFilter(): void
self::assertCount(3, $response);
}

public function testGetDocumentsWithFilterCorrectFieldFormat(): void
public function testGetDocumentsWithSort(): void
{
$fields = ['the', 'clash'];
$index = $this->createEmptyIndex($this->safeIndexName('movies'));
$index->updateSortableAttributes(['id', 'genre']);
$index->updateFilterableAttributes(['id', 'genre']);
$promise = $index->addDocuments(self::DOCUMENTS);
$index->waitForTask($promise['taskUid']);

$queryFields = (new DocumentsQuery())
->setFields($fields)
->setFilter(['id > 100'])
->toArray()['fields'];
$response = $index->getDocuments((new DocumentsQuery())->setSort(['genre:desc', 'id:asc']));
self::assertSame(2, $response[0]['id']);

self::assertSame($fields, $queryFields);
$response = $index->getDocuments((new DocumentsQuery())->setSort(['genre:desc', 'id:desc']));
self::assertSame(1344, $response[0]['id']);
}

public function testGetDocumentsWithFiltersFieldsAndSort(): void
{
$index = $this->createEmptyIndex($this->safeIndexName('movies'));
$index->updateSortableAttributes(['id', 'genre']);
$index->updateFilterableAttributes(['id', 'genre']);
$promise = $index->addDocuments(self::DOCUMENTS);
$index->waitForTask($promise['taskUid']);

$query = (new DocumentsQuery())
->setSort(['genre:desc', 'id:asc'])
->setFields(['id', 'title'])
->setFilter(['id > 2']);
$response = $index->getDocuments($query);
self::assertSame(123, $response[0]['id']);
self::assertSame(['id', 'title'], array_keys($response[0]));
}

public function testGetDocumentsWithoutFilterCorrectFieldsFormat(): void
public function testGetDocumentsWithFilterCorrectFieldFormat(): void
{
$fields = ['anti', 'flag'];
$fields = ['the', 'clash'];

$queryFields = (new DocumentsQuery())
->setFields($fields)
->setFilter(['id > 100'])
->toArray()['fields'];

self::assertSame(
implode(',', $fields),
$queryFields
);
self::assertSame($fields, $queryFields);
}

public function testGetDocumentsWithVector(): void
Expand Down
Loading