Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added isAOf(), isAOfAny(), notAOf() #106

Merged
merged 6 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Method | Description
`isInstanceOf($value, $class, $message = '')` | Check that a value is an `instanceof` a class
`isInstanceOfAny($value, array $classes, $message = '')` | Check that a value is an `instanceof` a at least one class on the array of classes
`notInstanceOf($value, $class, $message = '')` | Check that a value is not an `instanceof` a class
`isAOf($value, $class, $message = '')` | Check that a value is of the class or has one of its parents
`isAnyOf($value, array $classes, $message = '')` | Check that a value a at least one of the class or has one of its parents
`isNotA($value, $class, $message = '')` | Check that a value is not of the class or has not one of its parents
`isArrayAccessible($value, $message = '')` | Check that a value can be accessed as an array
`uniqueValues($values, $message = '')` | Check that the given array contains unique values

Expand Down
70 changes: 70 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
* @method static void nullOrIsInstanceOf($value, $class, $message = '')
* @method static void nullOrNotInstanceOf($value, $class, $message = '')
* @method static void nullOrIsInstanceOfAny($value, $classes, $message = '')
* @method static void nullOrIsAOf($value, $classes, $message = '')
* @method static void nullOrIsAnyOf($value, $classes, $message = '')
* @method static void nullOrIsNotA($value, $classes, $message = '')
* @method static void nullOrIsEmpty($value, $message = '')
* @method static void nullOrNotEmpty($value, $message = '')
* @method static void nullOrTrue($value, $message = '')
Expand Down Expand Up @@ -129,6 +132,9 @@
* @method static void allIsInstanceOf($values, $class, $message = '')
* @method static void allNotInstanceOf($values, $class, $message = '')
* @method static void allIsInstanceOfAny($values, $classes, $message = '')
* @method static void allIsAOf($values, $class, $message = '')
* @method static void allIsAnyOf($values, $class, $message = '')
* @method static void allIsNotA($values, $class, $message = '')
* @method static void allNull($values, $message = '')
* @method static void allNotNull($values, $message = '')
* @method static void allIsEmpty($values, $message = '')
Expand Down Expand Up @@ -590,6 +596,70 @@ public static function isInstanceOfAny($value, array $classes, $message = '')
));
}

/**
* @param object|string $value
* @param string $class
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isAOf($value, $class, $message = '')
{
static::string($class, 'Expected class as a string. Got: %s');

if (!\is_a($value, $class, \is_string($value))) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s',
static::typeToString($value),
$class
));
}
}

/**
* @param object|string $value
* @param string $class
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isNotA($value, $class, $message = '')
{
static::string($class, 'Expected class as a string. Got: %s');

if (\is_a($value, $class, \is_string($value))) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s',
static::typeToString($value),
$class
));
}
}

/**
* @param object|string $value
* @param string[] $classes
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function isAnyOf($value, array $classes, $message = '')
{
foreach ($classes as $class) {
static::string($class, 'Expected class as a string. Got: %s');

if (\is_a($value, $class, \is_string($value))) {
return;
}
}

static::reportInvalidArgument(sprintf(
$message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s',
static::typeToString($value),
\implode(', ', \array_map(array('static', 'valueToString'), $classes))
));
}

/**
* @psalm-assert empty $value
*
Expand Down
16 changes: 16 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ public function getTests()
array('isInstanceOfAny', array(new Exception(), array('ArrayAccess', 'Countable')), false),
array('isInstanceOfAny', array(123, array('stdClass')), false),
array('isInstanceOfAny', array(array(), array('stdClass')), false),
array('isAOf', array('stdClass', 'stdClass'), true),
array('isAOf', array('stdClass', 123), false),
array('isAOf', array('Iterator', 'ArrayIterator'), false),
array('isAOf', array(123, 'Iterator'), false),
array('isAOf', array(array(), 'Iterator'), false),
array('isAnyOf', array('ArrayIterator', array('Iterator', 'ArrayAccess')), true),
array('isAnyOf', array('ArrayIterator', array(123)), false),
array('isAnyOf', array('Exception', array('Exception', 'Countable')), true),
array('isAnyOf', array('Exception', array('ArrayAccess', 'Countable')), false),
array('isAnyOf', array(123, array('stdClass')), false),
array('isAnyOf', array(array(), array('stdClass')), false),
array('isNotA', array('stdClass', 'stdClass'), false),
array('isNotA', array('stdClass', 123), false),
array('isNotA', array('Iterator', 'ArrayIterator'), true),
array('isNotA', array(123, 'Iterator'), true),
array('isNotA', array(array(), 'Iterator'), true),
array('true', array(true), true),
array('true', array(false), false),
array('true', array(1), false),
Expand Down