anewt-developers team mailing list archive
-
anewt-developers team
-
Mailing list archive
-
Message #00261
[Branch ~uws/anewt/anewt.uws] Rev 1783: [core] Cleanup to_string() function
------------------------------------------------------------
revno: 1783
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Wed 2010-03-31 20:48:28 +0200
message:
[core] Cleanup to_string() function
Only accept a single argument, and handle __toString()
properly.
modified:
core/string.lib.php
core/string.test.php
--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws
Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription
=== modified file 'core/string.lib.php'
--- core/string.lib.php 2010-03-27 20:41:53 +0000
+++ core/string.lib.php 2010-03-31 18:48:28 +0000
@@ -450,86 +450,63 @@
* data types:
*
* - Simple values: strings, integers, floats, boolean (0 or 1).
- * - Any object implementing a render(), to_xhtml(), to_string() or toString() method.
+ * - Any object implementing a render(), __toString() method.
* - Numerical arrays containing any of the supported types are recursively
* converted to string values, separated by newlines.
*
* An error is thrown if the passed object cannot be handled.
*
- * \param $args
- * Any value supported by this function. You can also pass multiple
- * parameters.
+ * \param $value
+ * Any value supported by this function.
*
* \return
* String representation of the renderered object.
*/
-function to_string($args)
+function to_string($value)
{
- $args = func_get_args();
- $r = array();
- foreach ($args as $arg)
- {
- /* Strings are left as-is. Easy enough. */
- if (is_string($arg))
- {
- $r[] = $arg;
- continue;
- }
-
- /* Null values are skipped */
- if (is_null($arg))
- continue;
-
- /* Numbers are converted to strings. */
- if (is_integer($arg) || is_float($arg))
- {
- $r[] = (string) $arg;
- continue;
- }
-
- /* Boolean values are converted to 1 or 0. */
- if (is_bool($arg))
- {
- $r[] = $arg ? '1' : '0';
- continue;
- }
-
- /* Numerical arrays: recursively iterate over the items */
- if (is_numeric_array($arg))
- {
- $tmp = array();
- foreach (array_keys($arg) as $key)
- $tmp[] = to_string($arg[$key]);
-
- $r[] = implode(NL, $tmp);
- continue;
- }
-
- /* Handle objects */
- if (is_object($arg))
- {
- $found = false;
- foreach (array('render', 'to_string', 'toString') as $func)
+ /* Strings are left as-is. Easy enough. */
+ if (is_string($value))
+ return $value;
+
+ /* Null values are skipped */
+ if (is_null($value))
+ return '';
+
+ /* Numbers are converted to strings. */
+ if (is_integer($value) || is_float($value))
+ return (string) $value;
+
+ /* Boolean values are converted to 1 or 0. */
+ if (is_bool($value))
+ return $value ? '1' : '0';
+
+ /* Numerical arrays: recursively iterate over the items */
+ if (is_numeric_array($value))
+ {
+ $tmp = array();
+ foreach (array_keys($value) as $key)
+ $tmp[] = to_string($value[$key]);
+
+ return implode(NL, $tmp);
+ }
+
+ /* Handle objects */
+ if (is_object($value))
+ {
+ $found = false;
+ foreach (array('render', '__toString') as $func)
+ {
+ if (method_exists($value, $func))
{
- if (method_exists($arg, $func))
- {
- /* Call to_string() again because the method may not return
- * a string but (for instance) an array. */
- $r[] = to_string($arg->$func());
- $found = true;
- break; /* break out of inner loop */
- }
+ /* Call to_string() again because the method may not return
+ * a string but (for instance) an array. */
+ return to_string($value->$func());
}
- if ($found)
- continue; /* continue outer loop */
}
-
- /* All our attempts failed... throw an error */
- throw new AnewtException('Could not convert value to string: "%s"', $arg);
}
- /* Yay, done. */
- return implode(NL, $r);
+ /* All our attempts failed... throw an error */
+ throw new AnewtException('Could not convert value to string: "%s"', $value);
}
?>
=== modified file 'core/string.test.php'
--- core/string.test.php 2009-04-12 23:21:36 +0000
+++ core/string.test.php 2010-03-31 18:48:28 +0000
@@ -142,7 +142,7 @@
/* Test to_string */
class A { function render() { return 'foo'; } }
-class B { function to_string() { return array(1, "foo", 1.2, false); } }
+class B { function __toString() { return array(1, "foo", 1.2, false); } }
$a = new A();
$b = new B();
assert('to_string("foo") === "foo"');
@@ -151,6 +151,5 @@
assert('to_string(array("foo", "bar")) === "foo\nbar"');
assert('to_string($a) === "foo"');
assert('to_string($b) === "1\nfoo\n1.2\n0"');
-assert('to_string($a, "foo") === "foo\nfoo"');
?>