Merge file:///media/external_data/workspace/web/sport-group
[sport-group.git] / library / Zend / XmlRpc / Fault.php
blobb742440b5d69cd2ad072c9311ce84243aea906d3
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 * @package Zend_XmlRpc
16 * @subpackage Server
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
19 * @version $Id: Fault.php 16541 2009-07-07 06:59:03Z bkarwin $
22 /**
23 * Zend_XmlRpc_Value
25 require_once 'Zend/XmlRpc/Value.php';
27 /**
28 * XMLRPC Faults
30 * Container for XMLRPC faults, containing both a code and a message;
31 * additionally, has methods for determining if an XML response is an XMLRPC
32 * fault, as well as generating the XML for an XMLRPC fault response.
34 * To allow method chaining, you may only use the {@link getInstance()} factory
35 * to instantiate a Zend_XmlRpc_Server_Fault.
37 * @category Zend
38 * @package Zend_XmlRpc
39 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
40 * @license http://framework.zend.com/license/new-bsd New BSD License
42 class Zend_XmlRpc_Fault
44 /**
45 * Fault code
46 * @var int
48 protected $_code;
50 /**
51 * Fault character encoding
52 * @var string
54 protected $_encoding = 'UTF-8';
56 /**
57 * Fault message
58 * @var string
60 protected $_message;
62 /**
63 * Internal fault codes => messages
64 * @var array
66 protected $_internal = array(
67 404 => 'Unknown Error',
69 // 610 - 619 reflection errors
70 610 => 'Invalid method class',
71 611 => 'Unable to attach function or callback; not callable',
72 612 => 'Unable to load array; not an array',
73 613 => 'One or more method records are corrupt or otherwise unusable',
75 // 620 - 629 dispatch errors
76 620 => 'Method does not exist',
77 621 => 'Error instantiating class to invoke method',
78 622 => 'Method missing implementation',
79 623 => 'Calling parameters do not match signature',
81 // 630 - 639 request errors
82 630 => 'Unable to read request',
83 631 => 'Failed to parse request',
84 632 => 'Invalid request, no method passed; request must contain a \'methodName\' tag',
85 633 => 'Param must contain a value',
86 634 => 'Invalid method name',
87 635 => 'Invalid XML provided to request',
88 636 => 'Error creating xmlrpc value',
90 // 640 - 649 system.* errors
91 640 => 'Method does not exist',
93 // 650 - 659 response errors
94 650 => 'Invalid XML provided for response',
95 651 => 'Failed to parse response',
96 652 => 'Invalid response',
97 653 => 'Invalid XMLRPC value in response',
101 * Constructor
103 * @return Zend_XmlRpc_Fault
105 public function __construct($code = 404, $message = '')
107 $this->setCode($code);
108 $code = $this->getCode();
110 if (empty($message) && isset($this->_internal[$code])) {
111 $message = $this->_internal[$code];
112 } elseif (empty($message)) {
113 $message = 'Unknown error';
115 $this->setMessage($message);
119 * Set the fault code
121 * @param int $code
122 * @return Zend_XmlRpc_Fault
124 public function setCode($code)
126 $this->_code = (int) $code;
127 return $this;
131 * Return fault code
133 * @return int
135 public function getCode()
137 return $this->_code;
141 * Retrieve fault message
143 * @param string
144 * @return Zend_XmlRpc_Fault
146 public function setMessage($message)
148 $this->_message = (string) $message;
149 return $this;
153 * Retrieve fault message
155 * @return string
157 public function getMessage()
159 return $this->_message;
163 * Set encoding to use in fault response
165 * @param string $encoding
166 * @return Zend_XmlRpc_Fault
168 public function setEncoding($encoding)
170 $this->_encoding = $encoding;
171 return $this;
175 * Retrieve current fault encoding
177 * @return string
179 public function getEncoding()
181 return $this->_encoding;
185 * Load an XMLRPC fault from XML
187 * @param string $fault
188 * @return boolean Returns true if successfully loaded fault response, false
189 * if response was not a fault response
190 * @throws Zend_XmlRpc_Exception if no or faulty XML provided, or if fault
191 * response does not contain either code or message
193 public function loadXml($fault)
195 if (!is_string($fault)) {
196 require_once 'Zend/XmlRpc/Exception.php';
197 throw new Zend_XmlRpc_Exception('Invalid XML provided to fault');
200 try {
201 $xml = @new SimpleXMLElement($fault);
202 } catch (Exception $e) {
203 // Not valid XML
204 require_once 'Zend/XmlRpc/Exception.php';
205 throw new Zend_XmlRpc_Exception('Failed to parse XML fault: ' . $e->getMessage(), 500);
208 // Check for fault
209 if (!$xml->fault) {
210 // Not a fault
211 return false;
214 if (!$xml->fault->value->struct) {
215 // not a proper fault
216 require_once 'Zend/XmlRpc/Exception.php';
217 throw new Zend_XmlRpc_Exception('Invalid fault structure', 500);
220 $structXml = $xml->fault->value->asXML();
221 $structXml = preg_replace('/<\?xml version=.*?\?>/i', '', $structXml);
222 $struct = Zend_XmlRpc_Value::getXmlRpcValue(trim($structXml), Zend_XmlRpc_Value::XML_STRING);
223 $struct = $struct->getValue();
225 if (isset($struct['faultCode'])) {
226 $code = $struct['faultCode'];
228 if (isset($struct['faultString'])) {
229 $message = $struct['faultString'];
232 if (empty($code) && empty($message)) {
233 require_once 'Zend/XmlRpc/Exception.php';
234 throw new Zend_XmlRpc_Exception('Fault code and string required');
237 if (empty($code)) {
238 $code = '404';
241 if (empty($message)) {
242 if (isset($this->_internal[$code])) {
243 $message = $this->_internal[$code];
244 } else {
245 $message = 'Unknown Error';
249 $this->setCode($code);
250 $this->setMessage($message);
252 return true;
256 * Determine if an XML response is an XMLRPC fault
258 * @param string $xml
259 * @return boolean
261 public static function isFault($xml)
263 $fault = new self();
264 require_once 'Zend/XmlRpc/Exception.php';
265 try {
266 $isFault = $fault->loadXml($xml);
267 } catch (Zend_XmlRpc_Exception $e) {
268 $isFault = false;
271 return $isFault;
275 * Serialize fault to XML
277 * @return string
279 public function saveXML()
281 // Create fault value
282 $faultStruct = array(
283 'faultCode' => $this->getCode(),
284 'faultString' => $this->getMessage()
286 $value = Zend_XmlRpc_Value::getXmlRpcValue($faultStruct);
287 $valueDOM = new DOMDocument('1.0', $this->getEncoding());
288 $valueDOM->loadXML($value->saveXML());
290 // Build response XML
291 $dom = new DOMDocument('1.0', $this->getEncoding());
292 $r = $dom->appendChild($dom->createElement('methodResponse'));
293 $f = $r->appendChild($dom->createElement('fault'));
294 $f->appendChild($dom->importNode($valueDOM->documentElement, 1));
296 return $dom->saveXML();
300 * Return XML fault response
302 * @return string
304 public function __toString()
306 return $this->saveXML();