File indexing completed on 2024-12-22 05:37:04
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Service 0017 * @subpackage Twitter 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @see Zend_Http_Response 0025 */ 0026 // require_once 'Zend/Http/Response.php'; 0027 0028 /** 0029 * @see Zend_Json 0030 */ 0031 // require_once 'Zend/Json.php'; 0032 0033 /** 0034 * Representation of a response from Twitter. 0035 * 0036 * Provides: 0037 * 0038 * - method for testing if we have a successful call 0039 * - method for retrieving errors, if any 0040 * - method for retrieving the raw JSON 0041 * - method for retrieving the decoded response 0042 * - proxying to elements of the decoded response via property overloading 0043 * 0044 * @category Zend 0045 * @package Zend_Service 0046 * @subpackage Twitter 0047 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0048 * @license http://framework.zend.com/license/new-bsd New BSD License 0049 */ 0050 class Zend_Service_Twitter_Response 0051 { 0052 /** 0053 * @var Zend_Http_Response 0054 */ 0055 protected $httpResponse; 0056 0057 /** 0058 * @var array|stdClass 0059 */ 0060 protected $jsonBody; 0061 0062 /** 0063 * @var string 0064 */ 0065 protected $rawBody; 0066 0067 /** 0068 * Constructor 0069 * 0070 * Assigns the HTTP response to a property, as well as the body 0071 * representation. It then attempts to decode the body as JSON. 0072 * 0073 * @param Zend_Http_Response $httpResponse 0074 * @throws Zend_Service_Twitter_Exception if unable to decode JSON response 0075 */ 0076 public function __construct(Zend_Http_Response $httpResponse) 0077 { 0078 $this->httpResponse = $httpResponse; 0079 $this->rawBody = $httpResponse->getBody(); 0080 try { 0081 $jsonBody = Zend_Json::decode($this->rawBody, Zend_Json::TYPE_OBJECT); 0082 $this->jsonBody = $jsonBody; 0083 } catch (Zend_Json_Exception $e) { 0084 // require_once 'Zend/Service/Twitter/Exception.php'; 0085 throw new Zend_Service_Twitter_Exception(sprintf( 0086 'Unable to decode response from twitter: %s', 0087 $e->getMessage() 0088 ), 0, $e); 0089 } 0090 } 0091 0092 /** 0093 * Property overloading to JSON elements 0094 * 0095 * If a named property exists within the JSON response returned, 0096 * proxies to it. Otherwise, returns null. 0097 * 0098 * @param string $name 0099 * @return mixed 0100 */ 0101 public function __get($name) 0102 { 0103 if (null === $this->jsonBody) { 0104 return null; 0105 } 0106 if (!isset($this->jsonBody->{$name})) { 0107 return null; 0108 } 0109 return $this->jsonBody->{$name}; 0110 } 0111 0112 /** 0113 * Was the request successful? 0114 * 0115 * @return bool 0116 */ 0117 public function isSuccess() 0118 { 0119 return $this->httpResponse->isSuccessful(); 0120 } 0121 0122 /** 0123 * Did an error occur in the request? 0124 * 0125 * @return bool 0126 */ 0127 public function isError() 0128 { 0129 return !$this->httpResponse->isSuccessful(); 0130 } 0131 0132 /** 0133 * Retrieve the errors. 0134 * 0135 * Twitter _should_ return a standard error object, which contains an 0136 * "errors" property pointing to an array of errors. This method will 0137 * return that array if present, and raise an exception if not detected. 0138 * 0139 * If the response was successful, an empty array is returned. 0140 * 0141 * @return array 0142 * @throws Exception\DomainException if unable to detect structure of error response 0143 */ 0144 public function getErrors() 0145 { 0146 if (!$this->isError()) { 0147 return array(); 0148 } 0149 if (null === $this->jsonBody 0150 || !isset($this->jsonBody->errors) 0151 ) { 0152 // require_once 'Zend/Service/Twitter/Exception.php'; 0153 throw new Zend_Service_Twitter_Exception( 0154 'Either no JSON response received, or JSON error response is malformed; cannot return errors' 0155 ); 0156 } 0157 return $this->jsonBody->errors; 0158 } 0159 0160 /** 0161 * Retrieve the raw response body 0162 * 0163 * @return string 0164 */ 0165 public function getRawResponse() 0166 { 0167 return $this->rawBody; 0168 } 0169 0170 /** 0171 * Retun the decoded response body 0172 * 0173 * @return array|stdClass 0174 */ 0175 public function toValue() 0176 { 0177 return $this->jsonBody; 0178 } 0179 }