Skip to content

Commit 9d39dcb

Browse files
authored
Merge pull request #345 from meilisearch/indexes-v2
Indexes v2
2 parents e2006f1 + a91809b commit 9d39dcb

File tree

6 files changed

+165
-25
lines changed

6 files changed

+165
-25
lines changed

src/Contracts/IndexesQuery.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MeiliSearch\Contracts;
6+
7+
class IndexesQuery
8+
{
9+
private int $offset;
10+
private int $limit;
11+
12+
public function setOffset(int $offset): IndexesQuery
13+
{
14+
$this->offset = $offset;
15+
16+
return $this;
17+
}
18+
19+
public function setLimit(int $limit): IndexesQuery
20+
{
21+
$this->limit = $limit;
22+
23+
return $this;
24+
}
25+
26+
public function toArray(): array
27+
{
28+
return array_filter([
29+
'offset' => $this->offset ?? null,
30+
'limit' => $this->limit ?? null,
31+
], function ($item) { return null != $item || is_numeric($item); });
32+
}
33+
}

src/Contracts/IndexesResults.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MeiliSearch\Contracts;
6+
7+
class IndexesResults extends Data
8+
{
9+
private int $offset;
10+
private int $limit;
11+
private int $total;
12+
13+
public function __construct(array $params)
14+
{
15+
parent::__construct($params['results'] ?? []);
16+
17+
$this->offset = $params['offset'];
18+
$this->limit = $params['limit'];
19+
$this->total = $params['total'] ?? 0;
20+
}
21+
22+
/**
23+
* @return array<int, array>
24+
*/
25+
public function getResults(): array
26+
{
27+
return $this->data;
28+
}
29+
30+
public function getOffset(): int
31+
{
32+
return $this->offset;
33+
}
34+
35+
public function getLimit(): int
36+
{
37+
return $this->limit;
38+
}
39+
40+
public function getTotal(): int
41+
{
42+
return $this->total;
43+
}
44+
45+
public function toArray(): array
46+
{
47+
return [
48+
'results' => $this->data,
49+
'offset' => $this->offset,
50+
'limit' => $this->limit,
51+
'total' => $this->total,
52+
];
53+
}
54+
55+
public function count(): int
56+
{
57+
return \count($this->data);
58+
}
59+
}

src/Delegates/HandlesIndex.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44

55
namespace MeiliSearch\Delegates;
66

7+
use MeiliSearch\Contracts\IndexesQuery;
8+
use MeiliSearch\Contracts\IndexesResults;
79
use MeiliSearch\Endpoints\Indexes;
810

911
trait HandlesIndex
1012
{
11-
/**
12-
* @return Indexes[]
13-
*/
14-
public function getAllIndexes(): array
13+
public function getAllIndexes(IndexesQuery $options = null): IndexesResults
1514
{
16-
return $this->index->all();
15+
return $this->index->all($options ?? null);
1716
}
1817

19-
public function getAllRawIndexes(): array
18+
public function getAllRawIndexes(IndexesQuery $options = null): array
2019
{
21-
return $this->index->allRaw();
20+
return $this->index->allRaw($options ?? []);
2221
}
2322

2423
public function getRawIndex(string $uid): array

src/Endpoints/Indexes.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use MeiliSearch\Contracts\Endpoint;
1010
use MeiliSearch\Contracts\Http;
1111
use MeiliSearch\Contracts\Index\Settings;
12+
use MeiliSearch\Contracts\IndexesQuery;
13+
use MeiliSearch\Contracts\IndexesResults;
1214
use MeiliSearch\Endpoints\Delegates\HandlesDocuments;
1315
use MeiliSearch\Endpoints\Delegates\HandlesSettings;
1416
use MeiliSearch\Endpoints\Delegates\HandlesTasks;
@@ -74,19 +76,22 @@ public function create(string $uid, array $options = []): array
7476
return $this->http->post(self::PATH, $options);
7577
}
7678

77-
public function all(): array
79+
public function all(IndexesQuery $options = null): IndexesResults
7880
{
7981
$indexes = [];
80-
$response = $this->allRaw();
82+
$query = isset($options) ? $options->toArray() : [];
83+
$response = $this->allRaw($query);
8184

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

86-
return $indexes;
89+
$response['results'] = $indexes;
90+
91+
return new IndexesResults($response);
8792
}
8893

89-
public function allRaw(): array
94+
public function allRaw(array $options = []): array
9095
{
9196
return $this->http->get(self::PATH);
9297
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Contracts;
6+
7+
use MeiliSearch\Contracts\IndexesQuery;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class IndexesQueryTest extends TestCase
11+
{
12+
public function testToArrayWithSetOffsetAndSetLimit(): void
13+
{
14+
$data = (new IndexesQuery())->setLimit(10)->setOffset(18);
15+
16+
$this->assertEquals($data->toArray(), ['limit' => 10, 'offset' => 18]);
17+
}
18+
19+
public function testToArrayWithSetOffset(): void
20+
{
21+
$data = (new IndexesQuery())->setOffset(5);
22+
23+
$this->assertEquals($data->toArray(), ['offset' => 5]);
24+
}
25+
26+
public function testToArrayWithoutSet(): void
27+
{
28+
$data = new IndexesQuery();
29+
30+
$this->assertEquals($data->toArray(), []);
31+
}
32+
33+
public function testToArrayWithZeros(): void
34+
{
35+
$data = (new IndexesQuery())->setLimit(0)->setOffset(0);
36+
37+
$this->assertEquals($data->toArray(), ['limit' => 0, 'offset' => 0]);
38+
}
39+
}

tests/Endpoints/ClientTest.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tests\Endpoints;
66

77
use MeiliSearch\Client;
8+
use MeiliSearch\Contracts\IndexesQuery;
89
use MeiliSearch\Endpoints\Indexes;
910
use MeiliSearch\Exceptions\ApiException;
1011
use Tests\TestCase;
@@ -15,7 +16,7 @@ public function testClientIndexMethodsAlwaysReturnArray(): void
1516
{
1617
$index = $this->createEmptyIndex('index');
1718
/* @phpstan-ignore-next-line */
18-
$this->assertIsArray($this->client->getAllIndexes());
19+
$this->assertIsIterable($this->client->getAllIndexes());
1920
/* @phpstan-ignore-next-line */
2021
$this->assertIsArray($this->client->getAllRawIndexes()['results']);
2122
/* @phpstan-ignore-next-line */
@@ -38,6 +39,13 @@ public function testGetAllIndexesWhenEmpty(): void
3839
$this->assertEmpty($response);
3940
}
4041

42+
public function testGetAllIndexesWithPagination(): void
43+
{
44+
$response = $this->client->getAllIndexes((new IndexesQuery())->setLimit(1)->setOffset(99999));
45+
46+
$this->assertEmpty($response);
47+
}
48+
4149
public function testGetAllRawIndexesWhenEmpty(): void
4250
{
4351
$response = $this->client->getAllRawIndexes()['results'];
@@ -90,27 +98,24 @@ public function testGetAllIndexes(): void
9098
{
9199
$indexA = 'indexA';
92100
$indexB = 'indexB';
93-
$this->createEmptyIndex($indexA);
94-
$this->createEmptyIndex($indexB);
95-
96-
$response = $this->client->getAllIndexes();
97-
98-
$this->assertCount(2, $response);
101+
$response = $this->client->createIndex($indexA);
102+
$this->client->waitForTask($response['taskUid']);
103+
$response = $this->client->createIndex($indexB);
104+
$this->client->waitForTask($response['taskUid']);
99105

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

104-
$this->assertContains($indexA, $taskUids);
105-
$this->assertContains($indexB, $taskUids);
108+
$this->assertCount(2, $indexes);
106109
}
107110

108111
public function testGetAllRawIndexes(): void
109112
{
110113
$indexA = 'indexA';
111114
$indexB = 'indexB';
112-
$this->createEmptyIndex($indexA);
113-
$this->createEmptyIndex($indexB);
115+
$response = $this->client->createIndex($indexA);
116+
$this->client->waitForTask($response['taskUid']);
117+
$response = $this->client->createIndex($indexB);
118+
$this->client->waitForTask($response['taskUid']);
114119

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

0 commit comments

Comments
 (0)