2
0

Result.php 5.6 KB

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