diff --git a/README.md b/README.md index c34a0d74..b91c792b 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,8 @@ Method | Description `minCount($array, $min, $message = '')` | Check that an array contains at least a certain number of elements `maxCount($array, $max, $message = '')` | Check that an array contains at most a certain number of elements `countBetween($array, $min, $max, $message = '')` | Check that an array has a count in the given range +`isList($array, $message = '')` | Check that an array is a non-associative list +`isMap($array, $message = '')` | Check that an array is associative and has strings as keys ### Function Assertions diff --git a/src/Assert.php b/src/Assert.php index 76b3a660..db04bfbe 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -95,6 +95,8 @@ * @method static void nullOrCount($value, $key, $message = '') * @method static void nullOrMinCount($value, $min, $message = '') * @method static void nullOrMaxCount($value, $max, $message = '') + * @method static void nullOrIsList($value, $message = '') + * @method static void nullOrIsMap($value, $message = '') * @method static void nullOrCountBetween($value, $min, $max, $message = '') * @method static void nullOrUuid($values, $message = '') * @method static void nullOrThrows($expression, $class = 'Exception', $message = '') @@ -173,6 +175,8 @@ * @method static void allMinCount($values, $min, $message = '') * @method static void allMaxCount($values, $max, $message = '') * @method static void allCountBetween($values, $min, $max, $message = '') + * @method static void allIsList($values, $message = '') + * @method static void allIsMap($values, $message = '') * @method static void allUuid($values, $message = '') * @method static void allThrows($expressions, $class = 'Exception', $message = '') * @@ -1019,6 +1023,30 @@ public static function countBetween($array, $min, $max, $message = '') } } + public static function isList($array, $message = '') + { + if (!is_array($array) || !$array || array_keys($array) !== range(0, count($array) - 1)) { + static::reportInvalidArgument( + $message ?: 'Expected list - non-associative array.' + ); + } + } + + public static function isMap($array, $message = '') + { + if ( + !is_array($array) || + !$array || + array_keys($array) !== array_filter(array_keys($array), function ($key) { + return is_string($key); + }) + ) { + static::reportInvalidArgument( + $message ?: 'Expected map - associative array with string keys.' + ); + } + } + public static function uuid($value, $message = '') { $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 2c1ce683..bac79e4b 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -337,6 +337,14 @@ public function getTests() array('countBetween', array(array(0, 1, 2), 4, 5), false), array('countBetween', array(array(0, 1, 2), 1, 2), false), array('countBetween', array(array(0, 1, 2), 2, 5), true), + array('isList', array(array(1, 2, 3)), true), + array('isList', array(array()), false), + array('isList', array(array(0 => 1, 2 => 3)), false), + array('isList', array(array('key' => 1, 'foo' => 2)), false), + array('isMap', array(array('key' => 1, 'foo' => 2)), true), + array('isMap', array(array()), false), + array('isMap', array(array(1, 2, 3)), false), + array('isMap', array(array(0 => 1, 2 => 3)), false), array('uuid', array('00000000-0000-0000-0000-000000000000'), true), array('uuid', array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), true), array('uuid', array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'), true),