Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
 
5
/**
6
 * Function and class to dump XML_RPC_Value objects in a nice way
7
 *
8
 * Should be helpful as a normal var_dump(..) displays all internals which
9
 * doesn't really give you an overview due to too much information.
10
 *
11
 * @category   Web Services
12
 * @package    XML_RPC
13
 * @author     Christian Weiske <cweiske@php.net>
14
 * @license    http://www.php.net/license/3_01.txt  PHP License
15
 * @version    SVN: $Id: Dump.php 300962 2010-07-03 02:24:24Z danielc $
16
 * @link       http://pear.php.net/package/XML_RPC
17
 */
18
 
19
 
20
/**
21
 * Pull in the XML_RPC class
22
 */
23
require_once 'XML/RPC.php';
24
 
25
 
26
/**
27
 * Generates the dump of the XML_RPC_Value and echoes it
28
 *
29
 * @param object $value  the XML_RPC_Value object to dump
30
 *
31
 * @return void
32
 */
33
function XML_RPC_Dump($value)
34
{
35
    $dumper = new XML_RPC_Dump();
36
    echo $dumper->generateDump($value);
37
}
38
 
39
 
40
/**
41
 * Class which generates a dump of a XML_RPC_Value object
42
 *
43
 * @category   Web Services
44
 * @package    XML_RPC
45
 * @author     Christian Weiske <cweiske@php.net>
46
 * @license    http://www.php.net/license/3_01.txt  PHP License
47
 * @version    Release: @package_version@
48
 * @link       http://pear.php.net/package/XML_RPC
49
 */
50
class XML_RPC_Dump
51
{
52
    /**
53
     * The indentation array cache
54
     * @var array
55
     */
56
    var $arIndent      = array();
57
 
58
    /**
59
     * The spaces used for indenting the XML
60
     * @var string
61
     */
62
    var $strBaseIndent = '    ';
63
 
64
    /**
65
     * Returns the dump in XML format without printing it out
66
     *
67
     * @param object $value   the XML_RPC_Value object to dump
68
     * @param int    $nLevel  the level of indentation
69
     *
70
     * @return string  the dump
71
     */
72
    function generateDump($value, $nLevel = 0)
73
    {
74
        if (!is_object($value) || strtolower(get_class($value)) != 'xml_rpc_value') {
75
            require_once 'PEAR.php';
76
            PEAR::raiseError('Tried to dump non-XML_RPC_Value variable' . "\r\n",
77
                             0, PEAR_ERROR_PRINT);
78
            if (is_object($value)) {
79
                $strType = get_class($value);
80
            } else {
81
                $strType = gettype($value);
82
            }
83
            return $this->getIndent($nLevel) . 'NOT A XML_RPC_Value: '
84
                   . $strType . "\r\n";
85
        }
86
 
87
        switch ($value->kindOf()) {
88
        case 'struct':
89
            $ret = $this->genStruct($value, $nLevel);
90
            break;
91
        case 'array':
92
            $ret = $this->genArray($value, $nLevel);
93
            break;
94
        case 'scalar':
95
            $ret = $this->genScalar($value->scalarval(), $nLevel);
96
            break;
97
        default:
98
            require_once 'PEAR.php';
99
            PEAR::raiseError('Illegal type "' . $value->kindOf()
100
                             . '" in XML_RPC_Value' . "\r\n", 0,
101
                             PEAR_ERROR_PRINT);
102
        }
103
 
104
        return $ret;
105
    }
106
 
107
    /**
108
     * Returns the scalar value dump
109
     *
110
     * @param object $value   the scalar XML_RPC_Value object to dump
111
     * @param int    $nLevel  the level of indentation
112
     *
113
     * @return string  Dumped version of the scalar value
114
     */
115
    function genScalar($value, $nLevel)
116
    {
117
        if (gettype($value) == 'object') {
118
            $strClass = ' ' . get_class($value);
119
        } else {
120
            $strClass = '';
121
        }
122
        return $this->getIndent($nLevel) . gettype($value) . $strClass
123
               . ' ' . $value . "\r\n";
124
    }
125
 
126
    /**
127
     * Returns the dump of a struct
128
     *
129
     * @param object $value   the struct XML_RPC_Value object to dump
130
     * @param int    $nLevel  the level of indentation
131
     *
132
     * @return string  Dumped version of the scalar value
133
     */
134
    function genStruct($value, $nLevel)
135
    {
136
        $value->structreset();
137
        $strOutput = $this->getIndent($nLevel) . 'struct' . "\r\n";
138
        while (list($key, $keyval) = $value->structeach()) {
139
            $strOutput .= $this->getIndent($nLevel + 1) . $key . "\r\n";
140
            $strOutput .= $this->generateDump($keyval, $nLevel + 2);
141
        }
142
        return $strOutput;
143
    }
144
 
145
    /**
146
     * Returns the dump of an array
147
     *
148
     * @param object $value   the array XML_RPC_Value object to dump
149
     * @param int    $nLevel  the level of indentation
150
     *
151
     * @return string  Dumped version of the scalar value
152
     */
153
    function genArray($value, $nLevel)
154
    {
155
        $nSize     = $value->arraysize();
156
        $strOutput = $this->getIndent($nLevel) . 'array' . "\r\n";
157
        for($nA = 0; $nA < $nSize; $nA++) {
158
            $strOutput .= $this->getIndent($nLevel + 1) . $nA . "\r\n";
159
            $strOutput .= $this->generateDump($value->arraymem($nA),
160
                                              $nLevel + 2);
161
        }
162
        return $strOutput;
163
    }
164
 
165
    /**
166
     * Returns the indent for a specific level and caches it for faster use
167
     *
168
     * @param int $nLevel  the level
169
     *
170
     * @return string  the indented string
171
     */
172
    function getIndent($nLevel)
173
    {
174
        if (!isset($this->arIndent[$nLevel])) {
175
            $this->arIndent[$nLevel] = str_repeat($this->strBaseIndent, $nLevel);
176
        }
177
        return $this->arIndent[$nLevel];
178
    }
179
}
180
 
181
/*
182
 * Local variables:
183
 * tab-width: 4
184
 * c-basic-offset: 4
185
 * c-hanging-comment-ender-p: nil
186
 * End:
187
 */
188
 
189
?>