Response.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_Service
  17. * @subpackage Twitter
  18. * @copyright Copyright (c) 2005-2013 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. * @see Zend_Http_Response
  24. */
  25. require_once 'Zend/Http/Response.php';
  26. /**
  27. * @see Zend_Json
  28. */
  29. require_once 'Zend/Json.php';
  30. /**
  31. * Representation of a response from Twitter.
  32. *
  33. * Provides:
  34. *
  35. * - method for testing if we have a successful call
  36. * - method for retrieving errors, if any
  37. * - method for retrieving the raw JSON
  38. * - method for retrieving the decoded response
  39. * - proxying to elements of the decoded response via property overloading
  40. *
  41. * @category Zend
  42. * @package Zend_Service
  43. * @subpackage Twitter
  44. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_Service_Twitter_Response
  48. {
  49. /**
  50. * @var Zend_Http_Response
  51. */
  52. protected $httpResponse;
  53. /**
  54. * @var array|stdClass
  55. */
  56. protected $jsonBody;
  57. /**
  58. * @var string
  59. */
  60. protected $rawBody;
  61. /**
  62. * Constructor
  63. *
  64. * Assigns the HTTP response to a property, as well as the body
  65. * representation. It then attempts to decode the body as JSON.
  66. *
  67. * @param Zend_Http_Response $httpResponse
  68. * @throws Zend_Service_Twitter_Exception if unable to decode JSON response
  69. */
  70. public function __construct(Zend_Http_Response $httpResponse)
  71. {
  72. $this->httpResponse = $httpResponse;
  73. $this->rawBody = $httpResponse->getBody();
  74. try {
  75. $jsonBody = Zend_Json::decode($this->rawBody, Zend_Json::TYPE_OBJECT);
  76. $this->jsonBody = $jsonBody;
  77. } catch (Zend_Json_Exception $e) {
  78. require_once 'Zend/Service/Twitter/Exception.php';
  79. throw new Zend_Service_Twitter_Exception(sprintf(
  80. 'Unable to decode response from twitter: %s',
  81. $e->getMessage()
  82. ), 0, $e);
  83. }
  84. }
  85. /**
  86. * Property overloading to JSON elements
  87. *
  88. * If a named property exists within the JSON response returned,
  89. * proxies to it. Otherwise, returns null.
  90. *
  91. * @param string $name
  92. * @return mixed
  93. */
  94. public function __get($name)
  95. {
  96. if (null === $this->jsonBody) {
  97. return null;
  98. }
  99. if (!isset($this->jsonBody->{$name})) {
  100. return null;
  101. }
  102. return $this->jsonBody->{$name};
  103. }
  104. /**
  105. * Was the request successful?
  106. *
  107. * @return bool
  108. */
  109. public function isSuccess()
  110. {
  111. return $this->httpResponse->isSuccessful();
  112. }
  113. /**
  114. * Did an error occur in the request?
  115. *
  116. * @return bool
  117. */
  118. public function isError()
  119. {
  120. return !$this->httpResponse->isSuccessful();
  121. }
  122. /**
  123. * Retrieve the errors.
  124. *
  125. * Twitter _should_ return a standard error object, which contains an
  126. * "errors" property pointing to an array of errors. This method will
  127. * return that array if present, and raise an exception if not detected.
  128. *
  129. * If the response was successful, an empty array is returned.
  130. *
  131. * @return array
  132. * @throws Exception\DomainException if unable to detect structure of error response
  133. */
  134. public function getErrors()
  135. {
  136. if (!$this->isError()) {
  137. return array();
  138. }
  139. if (null === $this->jsonBody
  140. || !isset($this->jsonBody->errors)
  141. ) {
  142. require_once 'Zend/Service/Twitter/Exception.php';
  143. throw new Zend_Service_Twitter_Exception(
  144. 'Either no JSON response received, or JSON error response is malformed; cannot return errors'
  145. );
  146. }
  147. return $this->jsonBody->errors;
  148. }
  149. /**
  150. * Retrieve the raw response body
  151. *
  152. * @return string
  153. */
  154. public function getRawResponse()
  155. {
  156. return $this->rawBody;
  157. }
  158. /**
  159. * Retun the decoded response body
  160. *
  161. * @return array|stdClass
  162. */
  163. public function toValue()
  164. {
  165. return $this->jsonBody;
  166. }
  167. }