Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Air;
6
7
class Utils
8
{
9
    /**
10
     * Test if a value is a sequential array.
11
     *
12
     * @param mixed    $val            The tested value.
13
     * @param int|null $expectedLength If non null, only return true if the array length is exactly this value.
14
     * @return bool Whether the tested value is a sequential array.
15
     */
16
    public static function isSequentialArray($val, ?int $expectedLength = null): bool
17
    {
18
        if (!is_array($val)) {
19
            return false;
20
        }
21
22
        $cnt = count($val);
23
        if ($cnt === 0) {
24
            return $expectedLength === null || $expectedLength === 0;
25
        }
26
27
        // non-empty sequential array must have zero index, fast exit for 99% assoc arrays
28
        if (!array_key_exists(0, $val)) {
29
            return false;
30
        }
31
32
        // fast exit for length assertion
33
        if ($expectedLength !== null && $expectedLength !== $cnt) {
34
            return false;
35
        }
36
37
        $idx = 0;
38
        foreach ($val as $k => $v) {
39
            if ($k !== $idx++) {
40
                return false;
41
            }
42
        }
43
44
        return true;
45
    }
46
}
47