Skip to content
Snippets Groups Projects
Commit 85fa64de authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2579095 by Aki Tendo: Create Inspector::assertStringable - a shorthand...

Issue #2579095 by Aki Tendo: Create Inspector::assertStringable - a shorthand for (is_string($string) || (is_object($string) && method_exists($string, '__toString')
parent daad7b1c
No related branches found
No related tags found
No related merge requests found
...@@ -71,6 +71,9 @@ public static function assertAll(callable $callable, $traversable) { ...@@ -71,6 +71,9 @@ public static function assertAll(callable $callable, $traversable) {
/** /**
* Asserts that all members are strings. * 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 * @param mixed $traversable
* Variable to be examined. * Variable to be examined.
* *
...@@ -94,7 +97,7 @@ public static function assertAllStrings($traversable) { ...@@ -94,7 +97,7 @@ public static function assertAllStrings($traversable) {
public static function assertAllStringable($traversable) { public static function assertAllStringable($traversable) {
if (static::assertTraversable($traversable)) { if (static::assertTraversable($traversable)) {
foreach ($traversable as $member) { foreach ($traversable as $member) {
if (!(is_string($member) || (is_object($member) && method_exists($member, '__toString')))) { if (!static::assertStringable($member)) {
return FALSE; return FALSE;
} }
} }
...@@ -103,6 +106,22 @@ public static function assertAllStringable($traversable) { ...@@ -103,6 +106,22 @@ public static function assertAllStringable($traversable) {
return FALSE; 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. * Asserts that all members are arrays.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment