summaryrefslogtreecommitdiff
path: root/pear/tests/php_dump.php.inc
blob: 4f7a66629585ac4a261996ca9594851efa07d7f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
class PHP_Dump {
    var $_var;
    function PHP_Dump($var)
    {
        $this->_var = $var;
    }
    
    function toPHP()
    {
        return $this->_toUnknown($this->_var);
    }
    
    function _toUnknown($var, $indent = '    ')
    {
        switch (gettype($var)) {
            case 'array' :
                return $this->_toArray($var, $indent);
            case 'boolean' :
                return $this->_toBool($var, $indent);
            case 'double' :
            case 'integer' :
                return $this->_toNumber($var, $indent);
            case 'NULL' :
                return "{$indent}null";
            case 'string' :
                return $this->_toString($var, $indent);
        }
    }
    
    function _toString($var, $indent)
    {
        return $indent . '"' . addslashes($var) . '"';
    }
    
    function _toBool($var, $indent)
    {
        return $indent . ($var ? 'true' : 'false');
    }
    
    function _toNumber($var, $indent)
    {
        return $indent . $var;
    }
    
    function _toArray($var, $indent = '    ')
    {
        $ret = $indent . "array(\n";
        foreach ($var as $key => $value) {
            $ret .= $indent . ((is_int($key) || is_double($key)) ? $key : "'$key'") . " =>\n";
            $ret .= $this->_toUnknown($value, "$indent    ") . ",\n";
        }
        $ret .= $indent . ')';
        return $ret;
    }
}
?>