Skip to content

Commit

Permalink
Added isList and isMap assert which check that an array is a non-asso…
Browse files Browse the repository at this point in the history
…ciative list or an associative array with string keys respectively (webmozarts#54)
  • Loading branch information
TonyVlcek authored and Nyholm committed Dec 25, 2018
1 parent 318f16b commit 394d3e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '')
Expand Down Expand Up @@ -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 = '')
*
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 394d3e0

Please sign in to comment.