2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // Copyright (c) 2003 Laurent Bedubourg
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
24 * Static class used in common types internals.
27 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
32 * Generate a string representation of specified variable.
34 * @param mixed $var -- variable to represent in a string form.
37 function toString(&$var)
39 if (is_object($var)) {
40 return Types
::_objToString($var);
41 } elseif (is_array($var)) {
42 if (array_key_exists(0, $var) ||
count($var) == 0) {
43 return Types
::_arrayToString($var);
45 return Types
::_hashToString($var);
47 } elseif (is_resource($var)) {
48 return '#'.gettype($var).'#';
54 * Generate a string representation of an object calling its toString
55 * method of using its class name.
60 function _objToString(&$var)
62 if (method_exists($var, "toString")) {
63 return $var->toString();
65 return '<' . get_class($var) . ' instance>';
70 * Generate a string representation of a php array.
75 function _arrayToString(&$var)
78 foreach ($var as $val) {
79 $values[] = Types
::toString($val);
81 return '[' . join(', ', $values) . ']';
85 * Generate a string representation of an associative array.
90 function _hashToString(&$var)
93 foreach ($var as $key=>$val) {
94 $values[] = '\''. $key . '\': ' . Types
::toString($val);
96 return '{' . join(', ', $values) . '}';