Merge file:///media/external_data/workspace/web/sport-group
[sport-group.git] / library / Zend / XmlRpc / Request.php
blob2d4faedd6705d6a8c392b00b12913a747090db73
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Controller
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
21 /**
22 * Zend_XmlRpc_Value
24 require_once 'Zend/XmlRpc/Value.php';
26 /**
27 * Zend_XmlRpc_Fault
29 require_once 'Zend/XmlRpc/Fault.php';
31 /**
32 * XmlRpc Request object
34 * Encapsulates an XmlRpc request, holding the method call and all parameters.
35 * Provides accessors for these, as well as the ability to load from XML and to
36 * create the XML request string.
38 * Additionally, if errors occur setting the method or parsing XML, a fault is
39 * generated and stored in {@link $_fault}; developers may check for it using
40 * {@link isFault()} and {@link getFault()}.
42 * @category Zend
43 * @package Zend_XmlRpc
44 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
45 * @license http://framework.zend.com/license/new-bsd New BSD License
46 * @version $Id: Request.php 17786 2009-08-23 22:26:33Z lars $
48 class Zend_XmlRpc_Request
50 /**
51 * Request character encoding
52 * @var string
54 protected $_encoding = 'UTF-8';
56 /**
57 * Method to call
58 * @var string
60 protected $_method;
62 /**
63 * XML request
64 * @var string
66 protected $_xml;
68 /**
69 * Method parameters
70 * @var array
72 protected $_params = array();
74 /**
75 * Fault object, if any
76 * @var Zend_XmlRpc_Fault
78 protected $_fault = null;
80 /**
81 * XML-RPC type for each param
82 * @var array
84 protected $_types = array();
86 /**
87 * XML-RPC request params
88 * @var array
90 protected $_xmlRpcParams = array();
92 /**
93 * Create a new XML-RPC request
95 * @param string $method (optional)
96 * @param array $params (optional)
98 public function __construct($method = null, $params = null)
100 if ($method !== null) {
101 $this->setMethod($method);
104 if ($params !== null) {
105 $this->setParams($params);
111 * Set encoding to use in request
113 * @param string $encoding
114 * @return Zend_XmlRpc_Request
116 public function setEncoding($encoding)
118 $this->_encoding = $encoding;
119 return $this;
123 * Retrieve current request encoding
125 * @return string
127 public function getEncoding()
129 return $this->_encoding;
133 * Set method to call
135 * @param string $method
136 * @return boolean Returns true on success, false if method name is invalid
138 public function setMethod($method)
140 if (!is_string($method) || !preg_match('/^[a-z0-9_.:\/]+$/i', $method)) {
141 $this->_fault = new Zend_XmlRpc_Fault(634, 'Invalid method name ("' . $method . '")');
142 $this->_fault->setEncoding($this->getEncoding());
143 return false;
146 $this->_method = $method;
147 return true;
151 * Retrieve call method
153 * @return string
155 public function getMethod()
157 return $this->_method;
161 * Add a parameter to the parameter stack
163 * Adds a parameter to the parameter stack, associating it with the type
164 * $type if provided
166 * @param mixed $value
167 * @param string $type Optional; type hinting
168 * @return void
170 public function addParam($value, $type = null)
172 $this->_params[] = $value;
173 if (null === $type) {
174 // Detect type if not provided explicitly
175 if ($value instanceof Zend_XmlRpc_Value) {
176 $type = $value->getType();
177 } else {
178 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($value);
179 $type = $xmlRpcValue->getType();
182 $this->_types[] = $type;
183 $this->_xmlRpcParams[] = array('value' => $value, 'type' => $type);
187 * Set the parameters array
189 * If called with a single, array value, that array is used to set the
190 * parameters stack. If called with multiple values or a single non-array
191 * value, the arguments are used to set the parameters stack.
193 * Best is to call with array of the format, in order to allow type hinting
194 * when creating the XMLRPC values for each parameter:
195 * <code>
196 * $array = array(
197 * array(
198 * 'value' => $value,
199 * 'type' => $type
200 * )[, ... ]
201 * );
202 * </code>
204 * @access public
205 * @return void
207 public function setParams()
209 $argc = func_num_args();
210 $argv = func_get_args();
211 if (0 == $argc) {
212 return;
215 if ((1 == $argc) && is_array($argv[0])) {
216 $params = array();
217 $types = array();
218 $wellFormed = true;
219 foreach ($argv[0] as $arg) {
220 if (!is_array($arg) || !isset($arg['value'])) {
221 $wellFormed = false;
222 break;
224 $params[] = $arg['value'];
226 if (!isset($arg['type'])) {
227 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg['value']);
228 $arg['type'] = $xmlRpcValue->getType();
230 $types[] = $arg['type'];
232 if ($wellFormed) {
233 $this->_xmlRpcParams = $argv[0];
234 $this->_params = $params;
235 $this->_types = $types;
236 } else {
237 $this->_params = $argv[0];
238 $this->_types = array();
239 $xmlRpcParams = array();
240 foreach ($argv[0] as $arg) {
241 if ($arg instanceof Zend_XmlRpc_Value) {
242 $type = $arg->getType();
243 } else {
244 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
245 $type = $xmlRpcValue->getType();
247 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
248 $this->_types[] = $type;
250 $this->_xmlRpcParams = $xmlRpcParams;
252 return;
255 $this->_params = $argv;
256 $this->_types = array();
257 $xmlRpcParams = array();
258 foreach ($argv as $arg) {
259 if ($arg instanceof Zend_XmlRpc_Value) {
260 $type = $arg->getType();
261 } else {
262 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
263 $type = $xmlRpcValue->getType();
265 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
266 $this->_types[] = $type;
268 $this->_xmlRpcParams = $xmlRpcParams;
272 * Retrieve the array of parameters
274 * @return array
276 public function getParams()
278 return $this->_params;
282 * Return parameter types
284 * @return array
286 public function getTypes()
288 return $this->_types;
292 * Load XML and parse into request components
294 * @param string $request
295 * @return boolean True on success, false if an error occurred.
297 public function loadXml($request)
299 if (!is_string($request)) {
300 $this->_fault = new Zend_XmlRpc_Fault(635);
301 $this->_fault->setEncoding($this->getEncoding());
302 return false;
305 try {
306 $xml = @new SimpleXMLElement($request);
307 } catch (Exception $e) {
308 // Not valid XML
309 $this->_fault = new Zend_XmlRpc_Fault(631);
310 $this->_fault->setEncoding($this->getEncoding());
311 return false;
314 // Check for method name
315 if (empty($xml->methodName)) {
316 // Missing method name
317 $this->_fault = new Zend_XmlRpc_Fault(632);
318 $this->_fault->setEncoding($this->getEncoding());
319 return false;
322 $this->_method = (string) $xml->methodName;
324 // Check for parameters
325 if (!empty($xml->params)) {
326 $types = array();
327 $argv = array();
328 foreach ($xml->params->children() as $param) {
329 if (!isset($param->value)) {
330 $this->_fault = new Zend_XmlRpc_Fault(633);
331 $this->_fault->setEncoding($this->getEncoding());
332 return false;
335 try {
336 $param = Zend_XmlRpc_Value::getXmlRpcValue($param->value, Zend_XmlRpc_Value::XML_STRING);
337 $types[] = $param->getType();
338 $argv[] = $param->getValue();
339 } catch (Exception $e) {
340 $this->_fault = new Zend_XmlRpc_Fault(636);
341 $this->_fault->setEncoding($this->getEncoding());
342 return false;
346 $this->_types = $types;
347 $this->_params = $argv;
350 $this->_xml = $request;
352 return true;
356 * Does the current request contain errors and should it return a fault
357 * response?
359 * @return boolean
361 public function isFault()
363 return $this->_fault instanceof Zend_XmlRpc_Fault;
367 * Retrieve the fault response, if any
369 * @return null|Zend_XmlRpc_Fault
371 public function getFault()
373 return $this->_fault;
377 * Retrieve method parameters as XMLRPC values
379 * @return array
381 protected function _getXmlRpcParams()
383 $params = array();
384 if (is_array($this->_xmlRpcParams)) {
385 foreach ($this->_xmlRpcParams as $param) {
386 $value = $param['value'];
387 $type = isset($param['type']) ? $param['type'] : Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
389 if (!$value instanceof Zend_XmlRpc_Value) {
390 $value = Zend_XmlRpc_Value::getXmlRpcValue($value, $type);
392 $params[] = $value;
396 return $params;
400 * Create XML request
402 * @return string
404 public function saveXML()
406 $args = $this->_getXmlRpcParams();
407 $method = $this->getMethod();
409 $dom = new DOMDocument('1.0', $this->getEncoding());
410 $mCall = $dom->appendChild($dom->createElement('methodCall'));
411 $mName = $mCall->appendChild($dom->createElement('methodName', $method));
413 if (is_array($args) && count($args)) {
414 $params = $mCall->appendChild($dom->createElement('params'));
416 foreach ($args as $arg) {
417 /* @var $arg Zend_XmlRpc_Value */
418 $argDOM = new DOMDocument('1.0', $this->getEncoding());
419 $argDOM->loadXML($arg->saveXML());
421 $param = $params->appendChild($dom->createElement('param'));
422 $param->appendChild($dom->importNode($argDOM->documentElement, 1));
426 return $dom->saveXML();
430 * Return XML request
432 * @return string
434 public function __toString()
436 return $this->saveXML();