Issue #2579095 by Aki Tendo: Create Inspector::assertStringable - a shorthand for (is_string($string) || (is_object($string) && method_exists($string, '__toString')

8.0.x
Alex Pott 2015-10-05 11:22:22 +01:00
parent daad7b1c00
commit 85fa64de8d
1 changed files with 20 additions and 1 deletions

View File

@ -71,6 +71,9 @@ class Inspector {
/**
* Asserts that all members are strings.
*
* Use this only if it is vital that the members not be objects, otherwise
* test with ::assertAllStringable().
*
* @param mixed $traversable
* Variable to be examined.
*
@ -94,7 +97,7 @@ class Inspector {
public static function assertAllStringable($traversable) {
if (static::assertTraversable($traversable)) {
foreach ($traversable as $member) {
if (!(is_string($member) || (is_object($member) && method_exists($member, '__toString')))) {
if (!static::assertStringable($member)) {
return FALSE;
}
}
@ -103,6 +106,22 @@ class Inspector {
return FALSE;
}
/**
* Asserts argument is a string or an object castable to a string.
*
* Use this instead of is_string() alone unless the argument being an object
* in any way will cause a problem.
*
* @param mixed string
* Variable to be examined
*
* @return bool
* TRUE if $string is a string or an object castable to a string.
*/
public static function assertStringable($string) {
return is_string($string) || (is_object($string) && method_exists($string, '__toString'));
}
/**
* Asserts that all members are arrays.
*