Result.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Rest
  17. * @subpackage Client
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Xml/Security.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Rest
  26. * @subpackage Client
  27. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Rest_Client_Result implements IteratorAggregate {
  31. /**
  32. * @var SimpleXMLElement
  33. */
  34. protected $_sxml;
  35. /**
  36. * error information
  37. * @var string
  38. */
  39. protected $_errstr;
  40. /**
  41. * Constructor
  42. *
  43. * @param string $data XML Result
  44. * @return void
  45. */
  46. public function __construct($data)
  47. {
  48. set_error_handler(array($this, 'handleXmlErrors'));
  49. $this->_sxml = Zend_Xml_Security::scan($data);
  50. restore_error_handler();
  51. if($this->_sxml === false) {
  52. if ($this->_errstr === null) {
  53. $message = "An error occured while parsing the REST response with simplexml.";
  54. } else {
  55. $message = "REST Response Error: " . $this->_errstr;
  56. $this->_errstr = null;
  57. }
  58. require_once "Zend/Rest/Client/Result/Exception.php";
  59. throw new Zend_Rest_Client_Result_Exception($message);
  60. }
  61. }
  62. /**
  63. * Temporary error handler for parsing REST responses.
  64. *
  65. * @param int $errno
  66. * @param string $errstr
  67. * @param string $errfile
  68. * @param string $errline
  69. * @param array $errcontext
  70. * @return true
  71. */
  72. public function handleXmlErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
  73. {
  74. $this->_errstr = $errstr;
  75. return true;
  76. }
  77. /**
  78. * Casts a SimpleXMLElement to its appropriate PHP value
  79. *
  80. * @param SimpleXMLElement $value
  81. * @return mixed
  82. */
  83. public function toValue(SimpleXMLElement $value)
  84. {
  85. $node = dom_import_simplexml($value);
  86. return $node->nodeValue;
  87. }
  88. /**
  89. * Get Property Overload
  90. *
  91. * @param string $name
  92. * @return null|SimpleXMLElement|array Null if not found, SimpleXMLElement if only one value found, array of Zend_Rest_Client_Result objects otherwise
  93. */
  94. public function __get($name)
  95. {
  96. if (isset($this->_sxml->{$name})) {
  97. return $this->_sxml->{$name};
  98. }
  99. $result = $this->_sxml->xpath("//$name");
  100. $count = count($result);
  101. if ($count == 0) {
  102. return null;
  103. } elseif ($count == 1) {
  104. return $result[0];
  105. } else {
  106. return $result;
  107. }
  108. }
  109. /**
  110. * Cast properties to PHP values
  111. *
  112. * For arrays, loops through each element and casts to a value as well.
  113. *
  114. * @param string $method
  115. * @param array $args
  116. * @return mixed
  117. */
  118. public function __call($method, $args)
  119. {
  120. if (null !== ($value = $this->__get($method))) {
  121. if (!is_array($value)) {
  122. return $this->toValue($value);
  123. } else {
  124. $return = array();
  125. foreach ($value as $element) {
  126. $return[] = $this->toValue($element);
  127. }
  128. return $return;
  129. }
  130. }
  131. return null;
  132. }
  133. /**
  134. * Isset Overload
  135. *
  136. * @param string $name
  137. * @return boolean
  138. */
  139. public function __isset($name)
  140. {
  141. if (isset($this->_sxml->{$name})) {
  142. return true;
  143. }
  144. $result = $this->_sxml->xpath("//$name");
  145. if (sizeof($result) > 0) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * Implement IteratorAggregate::getIterator()
  152. *
  153. * @return SimpleXMLIterator
  154. */
  155. public function getIterator()
  156. {
  157. return $this->_sxml;
  158. }
  159. /**
  160. * Get Request Status
  161. *
  162. * @return boolean
  163. */
  164. public function getStatus()
  165. {
  166. $status = $this->_sxml->xpath('//status/text()');
  167. if ( !isset($status[0]) ) return false;
  168. $status = strtolower($status[0]);
  169. if (ctype_alpha($status) && $status == 'success') {
  170. return true;
  171. } elseif (ctype_alpha($status) && $status != 'success') {
  172. return false;
  173. } else {
  174. return (bool) $status;
  175. }
  176. }
  177. public function isError()
  178. {
  179. $status = $this->getStatus();
  180. if ($status) {
  181. return false;
  182. } else {
  183. return true;
  184. }
  185. }
  186. public function isSuccess()
  187. {
  188. $status = $this->getStatus();
  189. if ($status) {
  190. return true;
  191. } else {
  192. return false;
  193. }
  194. }
  195. /**
  196. * toString overload
  197. *
  198. * Be sure to only call this when the result is a single value!
  199. *
  200. * @return string
  201. */
  202. public function __toString()
  203. {
  204. if (!$this->getStatus()) {
  205. $message = $this->_sxml->xpath('//message');
  206. return (string) $message[0];
  207. } else {
  208. $result = $this->_sxml->xpath('//response');
  209. if (sizeof($result) > 1) {
  210. return (string) "An error occured.";
  211. } else {
  212. return (string) $result[0];
  213. }
  214. }
  215. }
  216. }