From 26903cee86d9fbc79a50eba76ba922aff513f69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20M=C3=B6nch?= Date: Fri, 14 Feb 2020 13:06:51 +0100 Subject: [PATCH] Added isAOf(), isAOfAny(), notAOf() (#106) * Added isAOf(), isAOfAny(), notAOf() Co-authored-by: Jean-Baptiste Delhommeau Co-authored-by: Gert de Pagter --- README.md | 3 ++ src/Assert.php | 70 ++++++++++++++++++++++++++++++++++++++++++++ tests/AssertTest.php | 16 ++++++++++ 3 files changed, 89 insertions(+) diff --git a/README.md b/README.md index 00b96811..6e5bb46b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Assert.php b/src/Assert.php index 81341952..aa7653e3 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -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 = '') @@ -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 = '') @@ -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 * diff --git a/tests/AssertTest.php b/tests/AssertTest.php index acf31164..ee850229 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -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),