minor bug fix that caused the default keyword not to work, small performance improvement
[lhc/web/wiklou.git] / PHPTAL-NP-0.7.0 / libs / Types.php
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 //
22
23 /**
24 * Static class used in common types internals.
25 *
26 * @static
27 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
28 */
29 class Types
30 {
31 /**
32 * Generate a string representation of specified variable.
33 *
34 * @param mixed $var -- variable to represent in a string form.
35 * @static
36 */
37 function toString(&$var)
38 {
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);
46 }
47 } elseif (is_resource($var)) {
48 return '#'.gettype($var).'#';
49 }
50 return $var;
51 }
52
53 /**
54 * Generate a string representation of an object calling its toString
55 * method of using its class name.
56 *
57 * @access protected
58 * @static
59 */
60 function _objToString(&$var)
61 {
62 if (method_exists($var, "toString")) {
63 return $var->toString();
64 } else {
65 return '<' . get_class($var) . ' instance>';
66 }
67 }
68
69 /**
70 * Generate a string representation of a php array.
71 *
72 * @access protected
73 * @static
74 */
75 function _arrayToString(&$var)
76 {
77 $values = array();
78 foreach ($var as $val) {
79 $values[] = Types::toString($val);
80 }
81 return '[' . join(', ', $values) . ']';
82 }
83
84 /**
85 * Generate a string representation of an associative array.
86 *
87 * @access protected
88 * @static
89 */
90 function _hashToString(&$var)
91 {
92 $values = array();
93 foreach ($var as $key=>$val) {
94 $values[] = '\''. $key . '\': ' . Types::toString($val);
95 }
96 return '{' . join(', ', $values) . '}';
97 }
98 }
99
100 ?>