summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-10-05 10:22:22 (GMT)
committerAlex Pott2015-10-05 10:22:22 (GMT)
commit85fa64de8d0a7af20a3ed4da8f32908c9c367eb5 (patch)
tree25af9ef554bf6fbcfda55981e38ede56e6d5d3d6
parentdaad7b1c00fcd033a555016f134875cf742b6a1f (diff)
Issue #2579095 by Aki Tendo: Create Inspector::assertStringable - a shorthand for (is_string($string) || (is_object($string) && method_exists($string, '__toString')
-rw-r--r--core/lib/Drupal/Component/Assertion/Inspector.php21
1 files changed, 20 insertions, 1 deletions
diff --git a/core/lib/Drupal/Component/Assertion/Inspector.php b/core/lib/Drupal/Component/Assertion/Inspector.php
index cb17632..7789754 100644
--- a/core/lib/Drupal/Component/Assertion/Inspector.php
+++ b/core/lib/Drupal/Component/Assertion/Inspector.php
@@ -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;
}
}
@@ -104,6 +107,22 @@ class Inspector {
}
/**
+ * 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.
*
* @param mixed $traversable