Skip to content

Commit e082831

Browse files
committed
feat: adds array_is_list polyfill
1 parent fb76c99 commit e082831

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
"autoload": {
3535
"psr-4": {
3636
"WordPress\\AiClient\\": "src/"
37-
}
37+
},
38+
"files": [
39+
"src/polyfills.php"
40+
]
3841
},
3942
"autoload-dev": {
4043
"psr-4": {

src/polyfills.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* Polyfills for PHP functions that may not be available in older versions.
5+
*
6+
* @since n.e.x.t
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
if (!function_exists('array_is_list')) {
12+
/**
13+
* Checks whether a given array is a list.
14+
*
15+
* An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
16+
*
17+
* @param array<mixed> $array The array to check.
18+
* @return bool True if the array is a list, false otherwise.
19+
*
20+
* @since n.e.x.t
21+
*/
22+
function array_is_list(array $array): bool
23+
{
24+
if ($array === []) {
25+
return true;
26+
}
27+
28+
$expectedKey = 0;
29+
foreach (array_keys($array) as $key) {
30+
if ($key !== $expectedKey) {
31+
return false;
32+
}
33+
$expectedKey++;
34+
}
35+
36+
return true;
37+
}
38+
}

0 commit comments

Comments
 (0)