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