Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / XmlRpc / Fault.php
blob2480baa3c006e8e782ec8240871121465d306404
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\XmlRpc;
12 use SimpleXMLElement;
14 /**
15 * XMLRPC Faults
17 * Container for XMLRPC faults, containing both a code and a message;
18 * additionally, has methods for determining if an XML response is an XMLRPC
19 * fault, as well as generating the XML for an XMLRPC fault response.
21 * To allow method chaining, you may only use the {@link getInstance()} factory
22 * to instantiate a Zend\XmlRpc\Server\Fault.
24 class Fault
26 /**
27 * Fault code
28 * @var int
30 protected $code;
32 /**
33 * Fault character encoding
34 * @var string
36 protected $encoding = 'UTF-8';
38 /**
39 * Fault message
40 * @var string
42 protected $message;
44 /**
45 * Internal fault codes => messages
46 * @var array
48 protected $internal = array(
49 404 => 'Unknown Error',
51 // 610 - 619 reflection errors
52 610 => 'Invalid method class',
53 611 => 'Unable to attach function or callback; not callable',
54 612 => 'Unable to load array; not an array',
55 613 => 'One or more method records are corrupt or otherwise unusable',
57 // 620 - 629 dispatch errors
58 620 => 'Method does not exist',
59 621 => 'Error instantiating class to invoke method',
60 622 => 'Method missing implementation',
61 623 => 'Calling parameters do not match signature',
63 // 630 - 639 request errors
64 630 => 'Unable to read request',
65 631 => 'Failed to parse request',
66 632 => 'Invalid request, no method passed; request must contain a \'methodName\' tag',
67 633 => 'Param must contain a value',
68 634 => 'Invalid method name',
69 635 => 'Invalid XML provided to request',
70 636 => 'Error creating xmlrpc value',
72 // 640 - 649 system.* errors
73 640 => 'Method does not exist',
75 // 650 - 659 response errors
76 650 => 'Invalid XML provided for response',
77 651 => 'Failed to parse response',
78 652 => 'Invalid response',
79 653 => 'Invalid XMLRPC value in response',
82 /**
83 * Constructor
86 public function __construct($code = 404, $message = '')
88 $this->setCode($code);
89 $code = $this->getCode();
91 if (empty($message) && isset($this->internal[$code])) {
92 $message = $this->internal[$code];
93 } elseif (empty($message)) {
94 $message = 'Unknown error';
96 $this->setMessage($message);
99 /**
100 * Set the fault code
102 * @param int $code
103 * @return Fault
105 public function setCode($code)
107 $this->code = (int) $code;
108 return $this;
112 * Return fault code
114 * @return int
116 public function getCode()
118 return $this->code;
122 * Retrieve fault message
124 * @param string
125 * @return Fault
127 public function setMessage($message)
129 $this->message = (string) $message;
130 return $this;
134 * Retrieve fault message
136 * @return string
138 public function getMessage()
140 return $this->message;
144 * Set encoding to use in fault response
146 * @param string $encoding
147 * @return Fault
149 public function setEncoding($encoding)
151 $this->encoding = $encoding;
152 AbstractValue::setEncoding($encoding);
153 return $this;
157 * Retrieve current fault encoding
159 * @return string
161 public function getEncoding()
163 return $this->encoding;
167 * Load an XMLRPC fault from XML
169 * @param string $fault
170 * @return bool Returns true if successfully loaded fault response, false
171 * if response was not a fault response
172 * @throws Exception\ExceptionInterface if no or faulty XML provided, or if fault
173 * response does not contain either code or message
175 public function loadXml($fault)
177 if (!is_string($fault)) {
178 throw new Exception\InvalidArgumentException('Invalid XML provided to fault');
181 $xmlErrorsFlag = libxml_use_internal_errors(true);
182 try {
183 $xml = new SimpleXMLElement($fault);
184 } catch (\Exception $e) {
185 // Not valid XML
186 throw new Exception\InvalidArgumentException('Failed to parse XML fault: ' . $e->getMessage(), 500, $e);
188 if (!$xml instanceof SimpleXMLElement) {
189 $errors = libxml_get_errors();
190 $errors = array_reduce($errors, function ($result, $item) {
191 if (empty($result)) {
192 return $item->message;
194 return $result . '; ' . $item->message;
195 }, '');
196 libxml_use_internal_errors($xmlErrorsFlag);
197 throw new Exception\InvalidArgumentException('Failed to parse XML fault: ' . $errors, 500);
199 libxml_use_internal_errors($xmlErrorsFlag);
201 // Check for fault
202 if (!$xml->fault) {
203 // Not a fault
204 return false;
207 if (!$xml->fault->value->struct) {
208 // not a proper fault
209 throw new Exception\InvalidArgumentException('Invalid fault structure', 500);
212 $structXml = $xml->fault->value->asXML();
213 $struct = AbstractValue::getXmlRpcValue($structXml, AbstractValue::XML_STRING);
214 $struct = $struct->getValue();
216 if (isset($struct['faultCode'])) {
217 $code = $struct['faultCode'];
219 if (isset($struct['faultString'])) {
220 $message = $struct['faultString'];
223 if (empty($code) && empty($message)) {
224 throw new Exception\InvalidArgumentException('Fault code and string required');
227 if (empty($code)) {
228 $code = '404';
231 if (empty($message)) {
232 if (isset($this->internal[$code])) {
233 $message = $this->internal[$code];
234 } else {
235 $message = 'Unknown Error';
239 $this->setCode($code);
240 $this->setMessage($message);
242 return true;
246 * Determine if an XML response is an XMLRPC fault
248 * @param string $xml
249 * @return bool
251 public static function isFault($xml)
253 $fault = new static();
254 try {
255 $isFault = $fault->loadXml($xml);
256 } catch (Exception\ExceptionInterface $e) {
257 $isFault = false;
260 return $isFault;
264 * Serialize fault to XML
266 * @return string
268 public function saveXml()
270 // Create fault value
271 $faultStruct = array(
272 'faultCode' => $this->getCode(),
273 'faultString' => $this->getMessage()
275 $value = AbstractValue::getXmlRpcValue($faultStruct);
277 $generator = AbstractValue::getGenerator();
278 $generator->openElement('methodResponse')
279 ->openElement('fault');
280 $value->generateXml();
281 $generator->closeElement('fault')
282 ->closeElement('methodResponse');
284 return $generator->flush();
288 * Return XML fault response
290 * @return string
292 public function __toString()
294 return $this->saveXML();