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
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 $
25 require_once 'Zend/XmlRpc/Value.php';
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.
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
51 * Fault character encoding
54 protected $_encoding = 'UTF-8';
63 * Internal fault codes => messages
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',
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);
122 * @return Zend_XmlRpc_Fault
124 public function setCode($code)
126 $this->_code
= (int) $code;
135 public function getCode()
141 * Retrieve fault message
144 * @return Zend_XmlRpc_Fault
146 public function setMessage($message)
148 $this->_message
= (string) $message;
153 * Retrieve fault message
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;
175 * Retrieve current fault encoding
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');
201 $xml = @new
SimpleXMLElement($fault);
202 } catch (Exception
$e) {
204 require_once 'Zend/XmlRpc/Exception.php';
205 throw new Zend_XmlRpc_Exception('Failed to parse XML fault: ' . $e->getMessage(), 500);
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');
241 if (empty($message)) {
242 if (isset($this->_internal
[$code])) {
243 $message = $this->_internal
[$code];
245 $message = 'Unknown Error';
249 $this->setCode($code);
250 $this->setMessage($message);
256 * Determine if an XML response is an XMLRPC fault
261 public static function isFault($xml)
264 require_once 'Zend/XmlRpc/Exception.php';
266 $isFault = $fault->loadXml($xml);
267 } catch (Zend_XmlRpc_Exception
$e) {
275 * Serialize fault to XML
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
304 public function __toString()
306 return $this->saveXML();