Skip to content

Commit

Permalink
Add validArrayKey assertion (webmozarts#140)
Browse files Browse the repository at this point in the history
* Add validArrayKey assertion
  • Loading branch information
mveldman authored and BackEndTea committed Oct 15, 2019
1 parent 5f69d7d commit a38a964
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '')
Expand Down Expand Up @@ -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 = '')
Expand Down Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit a38a964

Please sign in to comment.