removed old DaVinci files
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / Types.php
bloba39bcf0b7cb1e98485bfc0f85871e207a1858084
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // Copyright (c) 2003 Laurent Bedubourg
5 //
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.
10 //
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.
15 //
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
19 //
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
21 //
23 /**
24 * Static class used in common types internals.
26 * @static
27 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
29 class Types
31 /**
32 * Generate a string representation of specified variable.
34 * @param mixed $var -- variable to represent in a string form.
35 * @static
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);
44 } else {
45 return Types::_hashToString($var);
47 } elseif (is_resource($var)) {
48 return '#'.gettype($var).'#';
50 return $var;
53 /**
54 * Generate a string representation of an object calling its toString
55 * method of using its class name.
57 * @access protected
58 * @static
60 function _objToString(&$var)
62 if (method_exists($var, "toString")) {
63 return $var->toString();
64 } else {
65 return '<' . get_class($var) . ' instance>';
69 /**
70 * Generate a string representation of a php array.
72 * @access protected
73 * @static
75 function _arrayToString(&$var)
77 $values = array();
78 foreach ($var as $val) {
79 $values[] = Types::toString($val);
81 return '[' . join(', ', $values) . ']';
84 /**
85 * Generate a string representation of an associative array.
87 * @access protected
88 * @static
90 function _hashToString(&$var)
92 $values = array();
93 foreach ($var as $key=>$val) {
94 $values[] = '\''. $key . '\': ' . Types::toString($val);
96 return '{' . join(', ', $values) . '}';