Skip to content

Commit 751c3d0

Browse files
author
Benoit POLASZEK
committed
feat: add polyfill for array_is_list and make query handling more generic
1 parent f16b5c5 commit 751c3d0

File tree

6 files changed

+53
-7
lines changed

6 files changed

+53
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"psr-4": {
2929
"MeiliSearch\\": "src/",
3030
"Meilisearch\\": "src/"
31-
}
31+
},
32+
"files": ["src/polyfills.php"]
3233
},
3334
"autoload-dev": {
3435
"psr-4": {

phpstan.dist.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ parameters:
1010
paths:
1111
- src
1212
- tests
13+
tips:
14+
treatPhpDocTypesAsCertain: false
1315
additionalConstructors:
1416
- PHPUnit\Framework\TestCase::setUp

src/Endpoints/Delegates/HandlesDocuments.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ public function getDocuments(?DocumentsQuery $options = null): DocumentsResults
2929
if ($options->hasFilter()) {
3030
$response = $this->http->post(self::PATH.'/'.$this->uid.'/documents/fetch', $query);
3131
} else {
32-
if (isset($query['sort'])) {
33-
$query['sort'] = implode(',', $query['sort']);
34-
}
35-
if (isset($query['fields'])) {
36-
$query['fields'] = implode(',', $query['fields']);
37-
}
3832
$response = $this->http->get(self::PATH.'/'.$this->uid.'/documents', $query);
3933
}
4034

src/Http/Client.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ private function buildQueryString(array $queryParams = []): string
165165
if (\is_bool($value)) {
166166
$queryParams[$key] = $value ? 'true' : 'false';
167167
}
168+
if (\is_array($value) && array_is_list($value)) {
169+
$queryParams[$key] = implode(',', $value);
170+
}
168171
}
169172

170173
return \count($queryParams) > 0 ? '?'.http_build_query($queryParams) : '';

src/polyfills.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
if (!function_exists('array_is_list')) {
6+
/**
7+
* @param array<mixed, mixed> $array
8+
*
9+
* @see https://github.com/symfony/polyfill/blob/1.x/src/Php81/Php81.php
10+
*/
11+
function array_is_list(array $array): bool
12+
{
13+
if ([] === $array || $array === array_values($array)) {
14+
return true;
15+
}
16+
17+
$nextKey = -1;
18+
19+
foreach ($array as $k => $v) {
20+
if ($k !== ++$nextKey) {
21+
return false;
22+
}
23+
}
24+
25+
return true;
26+
}
27+
}

tests/PollyfillsTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use PHPUnit\Framework\Attributes\RequiresPhp;
8+
9+
class PollyfillsTest extends TestCase
10+
{
11+
#[RequiresPhp('< 8.0')]
12+
public function testArrayIsList(): void
13+
{
14+
self::assertTrue(array_is_list([])); // @phpstan-ignore-line
15+
self::assertTrue(array_is_list(['foo', 'bar'])); // @phpstan-ignore-line
16+
self::assertFalse(array_is_list(['foo' => 'bar'])); // @phpstan-ignore-line
17+
self::assertFalse(array_is_list([0 => 'foo', 'foo' => 'bar'])); // @phpstan-ignore-line
18+
}
19+
}

0 commit comments

Comments
 (0)