Result.php 5.5 KB

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