Skip to content
Merged
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
33 changes: 33 additions & 0 deletions src/Contracts/IndexesQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace MeiliSearch\Contracts;

class IndexesQuery
{
private int $offset;
private int $limit;

public function setOffset(int $offset): IndexesQuery
{
$this->offset = $offset;

return $this;
}

public function setLimit(int $limit): IndexesQuery
{
$this->limit = $limit;

return $this;
}

public function toArray(): array
{
return array_filter([
'offset' => $this->offset ?? null,
'limit' => $this->limit ?? null,
], function ($item) { return null != $item || is_numeric($item); });
}
}
59 changes: 59 additions & 0 deletions src/Contracts/IndexesResults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace MeiliSearch\Contracts;

class IndexesResults extends Data
{
private int $offset;
private int $limit;
private int $total;

public function __construct(array $params)
{
parent::__construct($params['results'] ?? []);

$this->offset = $params['offset'];
$this->limit = $params['limit'];
$this->total = $params['total'] ?? 0;
}

/**
* @return array<int, array>
*/
public function getResults(): array
{
return $this->data;
}

public function getOffset(): int
{
return $this->offset;
}

public function getLimit(): int
{
return $this->limit;
}

public function getTotal(): int
{
return $this->total;
}

public function toArray(): array
{
return [
'results' => $this->data,
'offset' => $this->offset,
'limit' => $this->limit,
'total' => $this->total,
];
}

public function count(): int
{
return \count($this->data);
}
}
13 changes: 6 additions & 7 deletions src/Delegates/HandlesIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@

namespace MeiliSearch\Delegates;

use MeiliSearch\Contracts\IndexesQuery;
use MeiliSearch\Contracts\IndexesResults;
use MeiliSearch\Endpoints\Indexes;

trait HandlesIndex
{
/**
* @return Indexes[]
*/
public function getAllIndexes(): array
public function getAllIndexes(IndexesQuery $options = null): IndexesResults
{
return $this->index->all();
return $this->index->all($options ?? null);
}

public function getAllRawIndexes(): array
public function getAllRawIndexes(IndexesQuery $options = null): array
{
return $this->index->allRaw();
return $this->index->allRaw($options ?? []);
}

public function getRawIndex(string $uid): array
Expand Down
13 changes: 9 additions & 4 deletions src/Endpoints/Indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use MeiliSearch\Contracts\Endpoint;
use MeiliSearch\Contracts\Http;
use MeiliSearch\Contracts\Index\Settings;
use MeiliSearch\Contracts\IndexesQuery;
use MeiliSearch\Contracts\IndexesResults;
use MeiliSearch\Endpoints\Delegates\HandlesDocuments;
use MeiliSearch\Endpoints\Delegates\HandlesSettings;
use MeiliSearch\Endpoints\Delegates\HandlesTasks;
Expand Down Expand Up @@ -74,19 +76,22 @@ public function create(string $uid, array $options = []): array
return $this->http->post(self::PATH, $options);
}

public function all(): array
public function all(IndexesQuery $options = null): IndexesResults
{
$indexes = [];
$response = $this->allRaw();
$query = isset($options) ? $options->toArray() : [];
$response = $this->allRaw($query);

foreach ($response['results'] as $index) {
$indexes[] = $this->newInstance($index);
}

return $indexes;
$response['results'] = $indexes;

return new IndexesResults($response);
}

public function allRaw(): array
public function allRaw(array $options = []): array
{
return $this->http->get(self::PATH);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Contracts/IndexesQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Contracts;

use MeiliSearch\Contracts\IndexesQuery;
use PHPUnit\Framework\TestCase;

class IndexesQueryTest extends TestCase
{
public function testToArrayWithSetOffsetAndSetLimit(): void
{
$data = (new IndexesQuery())->setLimit(10)->setOffset(18);

$this->assertEquals($data->toArray(), ['limit' => 10, 'offset' => 18]);
}

public function testToArrayWithSetOffset(): void
{
$data = (new IndexesQuery())->setOffset(5);

$this->assertEquals($data->toArray(), ['offset' => 5]);
}

public function testToArrayWithoutSet(): void
{
$data = new IndexesQuery();

$this->assertEquals($data->toArray(), []);
}

public function testToArrayWithZeros(): void
{
$data = (new IndexesQuery())->setLimit(0)->setOffset(0);

$this->assertEquals($data->toArray(), ['limit' => 0, 'offset' => 0]);
}
}
33 changes: 19 additions & 14 deletions tests/Endpoints/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Endpoints;

use MeiliSearch\Client;
use MeiliSearch\Contracts\IndexesQuery;
use MeiliSearch\Endpoints\Indexes;
use MeiliSearch\Exceptions\ApiException;
use Tests\TestCase;
Expand All @@ -15,7 +16,7 @@ public function testClientIndexMethodsAlwaysReturnArray(): void
{
$index = $this->createEmptyIndex('index');
/* @phpstan-ignore-next-line */
$this->assertIsArray($this->client->getAllIndexes());
$this->assertIsIterable($this->client->getAllIndexes());
/* @phpstan-ignore-next-line */
$this->assertIsArray($this->client->getAllRawIndexes()['results']);
/* @phpstan-ignore-next-line */
Expand All @@ -38,6 +39,13 @@ public function testGetAllIndexesWhenEmpty(): void
$this->assertEmpty($response);
}

public function testGetAllIndexesWithPagination(): void
{
$response = $this->client->getAllIndexes((new IndexesQuery())->setLimit(1)->setOffset(99999));

$this->assertEmpty($response);
}

public function testGetAllRawIndexesWhenEmpty(): void
{
$response = $this->client->getAllRawIndexes()['results'];
Expand Down Expand Up @@ -90,27 +98,24 @@ public function testGetAllIndexes(): void
{
$indexA = 'indexA';
$indexB = 'indexB';
$this->createEmptyIndex($indexA);
$this->createEmptyIndex($indexB);

$response = $this->client->getAllIndexes();

$this->assertCount(2, $response);
$response = $this->client->createIndex($indexA);
$this->client->waitForTask($response['taskUid']);
$response = $this->client->createIndex($indexB);
$this->client->waitForTask($response['taskUid']);

$taskUids = array_map(function ($index): ?string {
return $index->getUid();
}, $response);
$indexes = $this->client->getAllIndexes();

$this->assertContains($indexA, $taskUids);
$this->assertContains($indexB, $taskUids);
$this->assertCount(2, $indexes);
}

public function testGetAllRawIndexes(): void
{
$indexA = 'indexA';
$indexB = 'indexB';
$this->createEmptyIndex($indexA);
$this->createEmptyIndex($indexB);
$response = $this->client->createIndex($indexA);
$this->client->waitForTask($response['taskUid']);
$response = $this->client->createIndex($indexB);
$this->client->waitForTask($response['taskUid']);

$res = $this->client->getAllRawIndexes()['results'];

Expand Down