Result.php 5.8 KB

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