diff --git a/README.md b/README.md index bacddd04..2c9bc0da 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,7 @@ Method | Description -------------------------------------------------- | ------------------------------------------------------------------ `keyExists($array, $key, $message = '')` | Check that a key exists in an array `keyNotExists($array, $key, $message = '')` | Check that a key does not exist in an array +`validArrayKey($key, $message = '')` | Check that a value is a valid array key (int or string) `count($array, $number, $message = '')` | Check that an array contains a specific number of elements `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 diff --git a/src/Assert.php b/src/Assert.php index 4c9a334f..ee15dff3 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -95,6 +95,7 @@ * @method static void nullOrMethodNotExists($value, $method, $message = '') * @method static void nullOrKeyExists($value, $key, $message = '') * @method static void nullOrKeyNotExists($value, $key, $message = '') + * @method static void nullOrValidArrayKey($value, $message = '') * @method static void nullOrCount($value, $key, $message = '') * @method static void nullOrMinCount($value, $min, $message = '') * @method static void nullOrMaxCount($value, $max, $message = '') @@ -177,6 +178,7 @@ * @method static void allMethodNotExists($values, $method, $message = '') * @method static void allKeyExists($values, $key, $message = '') * @method static void allKeyNotExists($values, $key, $message = '') + * @method static void allValidArrayKey($values, $message = '') * @method static void allCount($values, $key, $message = '') * @method static void allMinCount($values, $min, $message = '') * @method static void allMaxCount($values, $max, $message = '') @@ -1602,6 +1604,24 @@ public static function keyNotExists($array, $key, $message = '') } } + /** + * Checks if a value is a valid array key (int or string). + * + * @param mixed $key + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function validArrayKey($value, $message = '') + { + if (!(\is_int($value) || \is_string($value))) { + static::reportInvalidArgument(\sprintf( + $message ?: 'Expected string or integer. Got: %s', + static::typeToString($value) + )); + } + } + /** * Does not check if $array is countable, this can generate a warning on php versions after 7.2. * diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 4546dab5..890475d5 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -403,6 +403,13 @@ public function getTests() array('keyNotExists', array(array('key' => 0), 'key'), false), array('keyNotExists', array(array('key' => null), 'key'), false), array('keyNotExists', array(array('key' => null), 'foo'), true), + array('validArrayKey', array('abcd'), true), + array('validArrayKey', array(1), true), + array('validArrayKey', array(false), false), + array('validArrayKey', array(true), false), + array('validArrayKey', array(new stdClass()), false), + array('validArrayKey', array(new ToStringClass('testString')), false), + array('validArrayKey', array(self::getResource()), false), array('count', array(array(0, 1, 2), 3), true), array('count', array(array(0, 1, 2), 2), false), array('minCount', array(array(0), 2), false),