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 foldmethod=marker: */
4
 
5
// LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{
6
 
7
/**
8
* +-----------------------------------------------------------------------------+
9
* | Copyright (c) 2004-2006 Sergio Goncalves Carvalho                                |
10
* +-----------------------------------------------------------------------------+
11
* | This file is part of XML_RPC2.                                              |
12
* |                                                                             |
13
* | XML_RPC2 is free software; you can redistribute it and/or modify            |
14
* | it under the terms of the GNU Lesser General Public License as published by |
15
* | the Free Software Foundation; either version 2.1 of the License, or         |
16
* | (at your option) any later version.                                         |
17
* |                                                                             |
18
* | XML_RPC2 is distributed in the hope that it will be useful,                 |
19
* | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
20
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
21
* | GNU Lesser General Public License for more details.                         |
22
* |                                                                             |
23
* | You should have received a copy of the GNU Lesser General Public License    |
24
* | along with XML_RPC2; if not, write to the Free Software                     |
25
* | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
26
* | 02111-1307 USA                                                              |
27
* +-----------------------------------------------------------------------------+
28
* | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com>                  |
29
* +-----------------------------------------------------------------------------+
30
*
31
* @category   XML
32
* @package    XML_RPC2
33
* @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>
34
* @copyright  2004-2006 Sergio Carvalho
35
* @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
36
* @version    CVS: $Id: Request.php 308701 2011-02-26 01:33:54Z sergiosgc $
37
* @link       http://pear.php.net/package/XML_RPC2
38
*/
39
 
40
// }}}
41
 
42
// dependencies {{{
43
require_once 'XML/RPC2/Exception.php';
44
require_once 'XML/RPC2/Backend/Php/Value.php';
45
// }}}
46
 
47
/**
48
 * XML_RPC request backend class. This class represents an XML_RPC request, exposing the methods
49
 * needed to encode/decode a request.
50
 *
51
 * @category   XML
52
 * @package    XML_RPC2
53
 * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>
54
 * @copyright  2004-2006 Sergio Carvalho
55
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
56
 * @link       http://pear.php.net/package/XML_RPC2
57
 */
58
class XML_RPC2_Backend_Php_Request
59
{
60
    // {{{ properties
61
 
62
    /**
63
     * Name of requested method
64
     *
65
     * @var mixed
66
     */
67
    private $_methodName = '';
68
 
69
    /**
70
     * request parameters
71
     *
72
     * @var array
73
     */
74
    private $_parameters = null;
75
 
76
    /**
77
     * encoding of the request
78
     *
79
     * @var string
80
     */
81
    private $_encoding = 'utf-8';
82
 
83
    // }}}
84
    // {{{ setParameters()
85
 
86
    /**
87
     * parameters property setter
88
     *
89
     * @param mixed value The new parameters
90
     */
91
    public function setParameters($value)
92
    {
93
        $this->_parameters = $value;
94
    }
95
 
96
    // }}}
97
    // {{{ addParameter()
98
 
99
    /**
100
     * parameters property appender
101
     *
102
     * @param mixed value The new parameter
103
     */
104
    public function addParameter($value)
105
    {
106
        $this->_parameters[] = $value;
107
    }
108
 
109
    // }}}
110
    // {{{ getParameters()
111
 
112
    /**
113
     * parameters property getter
114
     *
115
     * @return mixed The current parameters
116
     */
117
    public function getParameters()
118
    {
119
        return $this->_parameters;
120
    }
121
 
122
    // }}}
123
    // {{{ getMethodName()
124
 
125
    /**
126
     * method name getter
127
     *
128
     * @return string method name
129
     */
130
    public function getMethodName()
131
    {
132
        return $this->_methodName;
133
    }
134
 
135
    // }}}
136
    // {{{ constructor
137
 
138
    /**
139
     * Create a new xml-rpc request with the provided methodname
140
     *
141
     * @param string Name of method targeted by this xml-rpc request
142
     * @param string encoding of the request
143
     */
144
    function __construct($methodName, $encoding = 'utf-8')
145
    {
146
        $this->_methodName = $methodName;
147
        $this->setParameters(array());
148
        $this->_encoding = $encoding;
149
    }
150
 
151
    // }}}
152
    // {{{ encode()
153
 
154
    /**
155
     * Encode the request for transmission.
156
     *
157
     * @return string XML-encoded request (a full XML document)
158
     */
159
    public function encode()
160
    {
161
        $methodName = $this->_methodName;
162
        $parameters = $this->getParameters();
163
 
164
        $result = '<?xml version="1.0" encoding="' . $this->_encoding . '"?>';
165
        $result .= '<methodCall>';
166
        $result .= "<methodName>${methodName}</methodName>";
167
        $result .= '<params>';
168
        foreach($parameters as $parameter) {
169
            $result .= '<param><value>';
170
            $result .= ($parameter instanceof XML_RPC2_Backend_Php_Value) ? $parameter->encode() : XML_RPC2_Backend_Php_Value::createFromNative($parameter)->encode();
171
            $result .= '</value></param>';
172
        }
173
        $result .= '</params>';
174
        $result .= '</methodCall>';
175
        return $result;
176
    }
177
 
178
    // }}}
179
    // {{{ createFromDecode()
180
 
181
    /**
182
     * Decode a request from XML and construct a request object with the createFromDecoded values
183
     *
184
     * @param SimpleXMLElement The encoded XML-RPC request.
185
     * @return XML_RPC2_Backend_Php_Request The xml-rpc request, represented as an object instance
186
     */
187
    public static function createFromDecode($simpleXML)
188
    {
189
        $methodName = (string) $simpleXML->methodName;
190
        $params = array();
191
        foreach ($simpleXML->params->param as $param) {
192
            foreach ($param->value as $value) {
193
                $params[] = XML_RPC2_Backend_Php_Value::createFromDecode($value)->getNativeValue();
194
            }
195
        }
196
        $result = new XML_RPC2_Backend_Php_Request($methodName);
197
        $result->setParameters($params);
198
        return $result;
199
    }
200
 
201
    // }}}
202
 
203
}
204
 
205
?>