diff --git a/src/Assert.php b/src/Assert.php index 5451fe1..5853614 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -1822,11 +1822,24 @@ public static function countBetween($array, $min, $max, $message = '') */ public static function isList($array, $message = '') { - if (!\is_array($array) || $array !== \array_values($array)) { + if (!\is_array($array)) { static::reportInvalidArgument( $message ?: 'Expected list - non-associative array.' ); } + + if ($array === \array_values($array)) { + return; + } + + $nextKey = -1; + foreach ($array as $k => $v) { + if ($k !== ++$nextKey) { + static::reportInvalidArgument( + $message ?: 'Expected list - non-associative array.' + ); + } + } } /** diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 9565909..db3b421 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -51,6 +51,12 @@ public function getTests() { $resource = self::getResource(); + $nanList = array('key' => 2, NAN); + unset($nanList['key']); + + $normalList = array('foo' => 'b', 3); + unset($normalList['foo']); + return array( array('string', array('value'), true), array('string', array(''), true), @@ -495,6 +501,8 @@ public function getTests() array('isList', array(array(false)), true), array('isList', array(array(array(1), array(2))), true), array('isList', array(array(array('foo' => 'bar'), array('baz' => 'tab'))), true), + array('isList', array($nanList), true), + array('isList', array($normalList), true), array('isNonEmptyList', array(array(1, 2, 3)), true), array('isNonEmptyList', array(array()), false), array('isNonEmptyList', array(array(0 => 1, 2 => 3)), false),