*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / XmlRpc / Response.php
blob5dc057e04b2031395357d8ca4855cecb63555484
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 Response
34 * Container for accessing an XMLRPC return value and creating the XML response.
36 * @category Zend
37 * @package Zend_XmlRpc
38 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
40 * @version $Id: Response.php 16208 2009-06-21 19:19:26Z thomas $
42 class Zend_XmlRpc_Response
44 /**
45 * Return value
46 * @var mixed
48 protected $_return;
50 /**
51 * Return type
52 * @var string
54 protected $_type;
56 /**
57 * Response character encoding
58 * @var string
60 protected $_encoding = 'UTF-8';
62 /**
63 * Fault, if response is a fault response
64 * @var null|Zend_XmlRpc_Fault
66 protected $_fault = null;
68 /**
69 * Constructor
71 * Can optionally pass in the return value and type hinting; otherwise, the
72 * return value can be set via {@link setReturnValue()}.
74 * @param mixed $return
75 * @param string $type
76 * @return void
78 public function __construct($return = null, $type = null)
80 $this->setReturnValue($return, $type);
83 /**
84 * Set encoding to use in response
86 * @param string $encoding
87 * @return Zend_XmlRpc_Response
89 public function setEncoding($encoding)
91 $this->_encoding = $encoding;
92 return $this;
95 /**
96 * Retrieve current response encoding
98 * @return string
100 public function getEncoding()
102 return $this->_encoding;
106 * Set the return value
108 * Sets the return value, with optional type hinting if provided.
110 * @param mixed $value
111 * @param string $type
112 * @return void
114 public function setReturnValue($value, $type = null)
116 $this->_return = $value;
117 $this->_type = (string) $type;
121 * Retrieve the return value
123 * @return mixed
125 public function getReturnValue()
127 return $this->_return;
131 * Retrieve the XMLRPC value for the return value
133 * @return Zend_XmlRpc_Value
135 protected function _getXmlRpcReturn()
137 return Zend_XmlRpc_Value::getXmlRpcValue($this->_return);
141 * Is the response a fault response?
143 * @return boolean
145 public function isFault()
147 return $this->_fault instanceof Zend_XmlRpc_Fault;
151 * Returns the fault, if any.
153 * @return null|Zend_XmlRpc_Fault
155 public function getFault()
157 return $this->_fault;
161 * Load a response from an XML response
163 * Attempts to load a response from an XMLRPC response, autodetecting if it
164 * is a fault response.
166 * @param string $response
167 * @return boolean True if a valid XMLRPC response, false if a fault
168 * response or invalid input
170 public function loadXml($response)
172 if (!is_string($response)) {
173 $this->_fault = new Zend_XmlRpc_Fault(650);
174 $this->_fault->setEncoding($this->getEncoding());
175 return false;
178 try {
179 $xml = @new SimpleXMLElement($response);
180 } catch (Exception $e) {
181 // Not valid XML
182 $this->_fault = new Zend_XmlRpc_Fault(651);
183 $this->_fault->setEncoding($this->getEncoding());
184 return false;
187 if (!empty($xml->fault)) {
188 // fault response
189 $this->_fault = new Zend_XmlRpc_Fault();
190 $this->_fault->setEncoding($this->getEncoding());
191 $this->_fault->loadXml($response);
192 return false;
195 if (empty($xml->params)) {
196 // Invalid response
197 $this->_fault = new Zend_XmlRpc_Fault(652);
198 $this->_fault->setEncoding($this->getEncoding());
199 return false;
202 try {
203 if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
204 throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
206 $valueXml = $xml->params->param->value->asXML();
207 $valueXml = preg_replace('/<\?xml version=.*?\?>/i', '', $valueXml);
208 $value = Zend_XmlRpc_Value::getXmlRpcValue(trim($valueXml), Zend_XmlRpc_Value::XML_STRING);
209 } catch (Zend_XmlRpc_Value_Exception $e) {
210 $this->_fault = new Zend_XmlRpc_Fault(653);
211 $this->_fault->setEncoding($this->getEncoding());
212 return false;
215 $this->setReturnValue($value->getValue());
216 return true;
220 * Return response as XML
222 * @return string
224 public function saveXML()
226 $value = $this->_getXmlRpcReturn();
227 $valueDOM = new DOMDocument('1.0', $this->getEncoding());
228 $valueDOM->loadXML($value->saveXML());
230 $dom = new DOMDocument('1.0', $this->getEncoding());
231 $response = $dom->appendChild($dom->createElement('methodResponse'));
232 $params = $response->appendChild($dom->createElement('params'));
233 $param = $params->appendChild($dom->createElement('param'));
235 $param->appendChild($dom->importNode($valueDOM->documentElement, true));
237 return $dom->saveXML();
241 * Return XML response
243 * @return string
245 public function __toString()
247 return $this->saveXML();