/framework/vendor/zend/Zend/Rest/Client/Result.php

http://zoop.googlecode.com/ · PHP · 236 lines · 120 code · 22 blank · 94 comment · 28 complexity · 8451ff363dfc37a7cee1eac727db97c9 MD5 · raw file

  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Result.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Rest
  25. * @subpackage Client
  26. * @copyright Copyright (c) 2005-2010 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. $status = strtolower($status[0]);
  167. if (ctype_alpha($status) && $status == 'success') {
  168. return true;
  169. } elseif (ctype_alpha($status) && $status != 'success') {
  170. return false;
  171. } else {
  172. return (bool) $status;
  173. }
  174. }
  175. public function isError()
  176. {
  177. $status = $this->getStatus();
  178. if ($status) {
  179. return false;
  180. } else {
  181. return true;
  182. }
  183. }
  184. public function isSuccess()
  185. {
  186. $status = $this->getStatus();
  187. if ($status) {
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. }
  193. /**
  194. * toString overload
  195. *
  196. * Be sure to only call this when the result is a single value!
  197. *
  198. * @return string
  199. */
  200. public function __toString()
  201. {
  202. if (!$this->getStatus()) {
  203. $message = $this->_sxml->xpath('//message');
  204. return (string) $message[0];
  205. } else {
  206. $result = $this->_sxml->xpath('//response');
  207. if (sizeof($result) > 1) {
  208. return (string) "An error occured.";
  209. } else {
  210. return (string) $result[0];
  211. }
  212. }
  213. }
  214. }