Index: lang.base.php =================================================================== RCS file: /home/cvs/repositories/xp/skeleton/lang.base.php,v retrieving revision 1.62 diff -u -r1.62 lang.base.php --- lang.base.php 19 Nov 2004 02:07:58 -0000 1.62 +++ lang.base.php 22 Nov 2004 22:45:30 -0000 @@ -207,7 +207,7 @@ } // }}} - // {{{ null throw (lang.Exception e) + // {{{ null throw (&lang.Exception e) // throws an exception function &throw(&$e) { $exceptions= &xp::registry('exceptions'); @@ -217,45 +217,72 @@ } // }}} - // {{{ mixed cast (&mixed var, mixed type default NULL) + // {{{ bools is_subclass(&object var, string type) + // Returns TRUE if type is a subclass of var's class. + function is_subclass(&$var, $type) { + $c= get_class($var); + do { + if (!$type= get_parent_class($type)) return FALSE; + } while ($c != $type); + return TRUE; + } + // }}} + + // {{{ mixed &cast (mixed argument, mixed type) // Casts. If var === NULL, it won't be touched - function &cast(&$var, $type= NULL) { + function &cast($var, $type) { if (NULL === $var) return NULL; switch ($type) { - case NULL: - break; - - case 'int': - case 'integer': - case 'float': - case 'double': case 'string': - case 'bool': - case 'null': - if (is_a($var, 'Object')) $var= $var->toString(); - settype($var, $type); - break; + if (is_a($var, 'Object')) return $var->toString(); + if (is_array($var)) { + return throw(new InvalidCastException('Cannot cast arrays to strings')); + } + return (string)$var; - case 'array': - case 'object': - settype($var, $type); - break; + case 'bool': case 'boolean': + if (is_a($var, 'Object') || is_array($var)) { + return throw(new InvalidCastException('Cannot cast arrays or objects to booleans')); + } + return (boolean)$var; + case 'int': case 'integer': + if (is_a($var, 'Object') || is_array($var)) { + return throw(new InvalidCastException('Cannot cast arrays or objects to integers')); + } + return (int)$var; + + case 'float': case 'double': + if (is_a($var, 'Object') || is_array($var)) { + return throw(new InvalidCastException('Cannot cast arrays or objects to doubles')); + } + return (double)$var; + + case 'array': + if (is_a($var, 'Collection')) return $var->values(); + return (array)$var; + default: - // Cast to an object of "$type" - $o= &new $type; - if (is_object($var) || is_array($var)) { - foreach ($var as $k => $v) { - $o->$k= $v; - } - } else { - $o->scalar= $var; + $name= xp::reflect($type); + if (!class_exists($name)) { + return throw(new ClassNotFoundException('Class "'.$type.'" not found')); + } + if (is_array($var)) { + $o= &unserialize('O:'.strlen($name).':"'.$name.'"'.substr(serialize($var), 1)); + $o->__id= microtime(); + return $o; + } + if (!is_a($var, $name) && !is_subclass($var, $name)) { + return throw(new ClassCastException(xp::typeOf($var).' is not a "'.$type.'"')); } - return $o; - break; + $length= strlen(get_class($var)); + return unserialize('O:'.strlen($name).':"'.$name.'"'.substr( + serialize($var), + 2 + strlen($length) + 2 + $length + 1 + )); } - return $var; + // Should never reach this point } // }}} @@ -370,7 +397,9 @@ 'lang.IllegalArgumentException', 'lang.IllegalStateException', 'lang.FormatException', - 'lang.ClassLoader' + 'lang.ClassLoader', + 'lang.InvalidCastException', + 'lang.ClassCastException' ); // }}} Index: lang/ClassCastException.class.php =================================================================== RCS file: lang/ClassCastException.class.php diff -N lang/ClassCastException.class.php --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lang/ClassCastException.class.php 22 Nov 2004 22:45:30 -0000 @@ -0,0 +1,15 @@ + Index: lang/InvalidCastException.class.php =================================================================== RCS file: lang/InvalidCastException.class.php diff -N lang/InvalidCastException.class.php --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lang/InvalidCastException.class.php 22 Nov 2004 22:45:30 -0000 @@ -0,0 +1,15 @@ +