[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / XmlRpc / Request.php
blob5a05073fca2853e20bbc67a7324ca4696e35a63b
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-2010 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
45 * @license http://framework.zend.com/license/new-bsd New BSD License
46 * @version $Id$
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 Zend_XmlRpc_Value::setEncoding($encoding);
120 return $this;
124 * Retrieve current request encoding
126 * @return string
128 public function getEncoding()
130 return $this->_encoding;
134 * Set method to call
136 * @param string $method
137 * @return boolean Returns true on success, false if method name is invalid
139 public function setMethod($method)
141 if (!is_string($method) || !preg_match('/^[a-z0-9_.:\/]+$/i', $method)) {
142 $this->_fault = new Zend_XmlRpc_Fault(634, 'Invalid method name ("' . $method . '")');
143 $this->_fault->setEncoding($this->getEncoding());
144 return false;
147 $this->_method = $method;
148 return true;
152 * Retrieve call method
154 * @return string
156 public function getMethod()
158 return $this->_method;
162 * Add a parameter to the parameter stack
164 * Adds a parameter to the parameter stack, associating it with the type
165 * $type if provided
167 * @param mixed $value
168 * @param string $type Optional; type hinting
169 * @return void
171 public function addParam($value, $type = null)
173 $this->_params[] = $value;
174 if (null === $type) {
175 // Detect type if not provided explicitly
176 if ($value instanceof Zend_XmlRpc_Value) {
177 $type = $value->getType();
178 } else {
179 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($value);
180 $type = $xmlRpcValue->getType();
183 $this->_types[] = $type;
184 $this->_xmlRpcParams[] = array('value' => $value, 'type' => $type);
188 * Set the parameters array
190 * If called with a single, array value, that array is used to set the
191 * parameters stack. If called with multiple values or a single non-array
192 * value, the arguments are used to set the parameters stack.
194 * Best is to call with array of the format, in order to allow type hinting
195 * when creating the XMLRPC values for each parameter:
196 * <code>
197 * $array = array(
198 * array(
199 * 'value' => $value,
200 * 'type' => $type
201 * )[, ... ]
202 * );
203 * </code>
205 * @access public
206 * @return void
208 public function setParams()
210 $argc = func_num_args();
211 $argv = func_get_args();
212 if (0 == $argc) {
213 return;
216 if ((1 == $argc) && is_array($argv[0])) {
217 $params = array();
218 $types = array();
219 $wellFormed = true;
220 foreach ($argv[0] as $arg) {
221 if (!is_array($arg) || !isset($arg['value'])) {
222 $wellFormed = false;
223 break;
225 $params[] = $arg['value'];
227 if (!isset($arg['type'])) {
228 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg['value']);
229 $arg['type'] = $xmlRpcValue->getType();
231 $types[] = $arg['type'];
233 if ($wellFormed) {
234 $this->_xmlRpcParams = $argv[0];
235 $this->_params = $params;
236 $this->_types = $types;
237 } else {
238 $this->_params = $argv[0];
239 $this->_types = array();
240 $xmlRpcParams = array();
241 foreach ($argv[0] as $arg) {
242 if ($arg instanceof Zend_XmlRpc_Value) {
243 $type = $arg->getType();
244 } else {
245 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
246 $type = $xmlRpcValue->getType();
248 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
249 $this->_types[] = $type;
251 $this->_xmlRpcParams = $xmlRpcParams;
253 return;
256 $this->_params = $argv;
257 $this->_types = array();
258 $xmlRpcParams = array();
259 foreach ($argv as $arg) {
260 if ($arg instanceof Zend_XmlRpc_Value) {
261 $type = $arg->getType();
262 } else {
263 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
264 $type = $xmlRpcValue->getType();
266 $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
267 $this->_types[] = $type;
269 $this->_xmlRpcParams = $xmlRpcParams;
273 * Retrieve the array of parameters
275 * @return array
277 public function getParams()
279 return $this->_params;
283 * Return parameter types
285 * @return array
287 public function getTypes()
289 return $this->_types;
293 * Load XML and parse into request components
295 * @param string $request
296 * @return boolean True on success, false if an error occurred.
298 public function loadXml($request)
300 if (!is_string($request)) {
301 $this->_fault = new Zend_XmlRpc_Fault(635);
302 $this->_fault->setEncoding($this->getEncoding());
303 return false;
306 try {
307 $xml = new SimpleXMLElement($request);
308 } catch (Exception $e) {
309 // Not valid XML
310 $this->_fault = new Zend_XmlRpc_Fault(631);
311 $this->_fault->setEncoding($this->getEncoding());
312 return false;
315 // Check for method name
316 if (empty($xml->methodName)) {
317 // Missing method name
318 $this->_fault = new Zend_XmlRpc_Fault(632);
319 $this->_fault->setEncoding($this->getEncoding());
320 return false;
323 $this->_method = (string) $xml->methodName;
325 // Check for parameters
326 if (!empty($xml->params)) {
327 $types = array();
328 $argv = array();
329 foreach ($xml->params->children() as $param) {
330 if (!isset($param->value)) {
331 $this->_fault = new Zend_XmlRpc_Fault(633);
332 $this->_fault->setEncoding($this->getEncoding());
333 return false;
336 try {
337 $param = Zend_XmlRpc_Value::getXmlRpcValue($param->value, Zend_XmlRpc_Value::XML_STRING);
338 $types[] = $param->getType();
339 $argv[] = $param->getValue();
340 } catch (Exception $e) {
341 $this->_fault = new Zend_XmlRpc_Fault(636);
342 $this->_fault->setEncoding($this->getEncoding());
343 return false;
347 $this->_types = $types;
348 $this->_params = $argv;
351 $this->_xml = $request;
353 return true;
357 * Does the current request contain errors and should it return a fault
358 * response?
360 * @return boolean
362 public function isFault()
364 return $this->_fault instanceof Zend_XmlRpc_Fault;
368 * Retrieve the fault response, if any
370 * @return null|Zend_XmlRpc_Fault
372 public function getFault()
374 return $this->_fault;
378 * Retrieve method parameters as XMLRPC values
380 * @return array
382 protected function _getXmlRpcParams()
384 $params = array();
385 if (is_array($this->_xmlRpcParams)) {
386 foreach ($this->_xmlRpcParams as $param) {
387 $value = $param['value'];
388 $type = isset($param['type']) ? $param['type'] : Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
390 if (!$value instanceof Zend_XmlRpc_Value) {
391 $value = Zend_XmlRpc_Value::getXmlRpcValue($value, $type);
393 $params[] = $value;
397 return $params;
401 * Create XML request
403 * @return string
405 public function saveXml()
407 $args = $this->_getXmlRpcParams();
408 $method = $this->getMethod();
410 $generator = Zend_XmlRpc_Value::getGenerator();
411 $generator->openElement('methodCall')
412 ->openElement('methodName', $method)
413 ->closeElement('methodName');
415 if (is_array($args) && count($args)) {
416 $generator->openElement('params');
418 foreach ($args as $arg) {
419 $generator->openElement('param');
420 $arg->generateXml();
421 $generator->closeElement('param');
423 $generator->closeElement('params');
425 $generator->closeElement('methodCall');
427 return $generator->flush();
431 * Return XML request
433 * @return string
435 public function __toString()
437 return $this->saveXML();