diff -r 877f952ae2bd -r 6b6c2214f778 web/lib/Zend/Service/Twitter/Response.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/lib/Zend/Service/Twitter/Response.php Thu Mar 21 19:52:38 2013 +0100 @@ -0,0 +1,179 @@ +httpResponse = $httpResponse; + $this->rawBody = $httpResponse->getBody(); + try { + $jsonBody = Zend_Json::decode($this->rawBody, Zend_Json::TYPE_OBJECT); + $this->jsonBody = $jsonBody; + } catch (Zend_Json_Exception $e) { + require_once 'Zend/Service/Twitter/Exception.php'; + throw new Zend_Service_Twitter_Exception(sprintf( + 'Unable to decode response from twitter: %s', + $e->getMessage() + ), 0, $e); + } + } + + /** + * Property overloading to JSON elements + * + * If a named property exists within the JSON response returned, + * proxies to it. Otherwise, returns null. + * + * @param string $name + * @return mixed + */ + public function __get($name) + { + if (null === $this->jsonBody) { + return null; + } + if (!isset($this->jsonBody->{$name})) { + return null; + } + return $this->jsonBody->{$name}; + } + + /** + * Was the request successful? + * + * @return bool + */ + public function isSuccess() + { + return $this->httpResponse->isSuccessful(); + } + + /** + * Did an error occur in the request? + * + * @return bool + */ + public function isError() + { + return !$this->httpResponse->isSuccessful(); + } + + /** + * Retrieve the errors. + * + * Twitter _should_ return a standard error object, which contains an + * "errors" property pointing to an array of errors. This method will + * return that array if present, and raise an exception if not detected. + * + * If the response was successful, an empty array is returned. + * + * @return array + * @throws Exception\DomainException if unable to detect structure of error response + */ + public function getErrors() + { + if (!$this->isError()) { + return array(); + } + if (null === $this->jsonBody + || !isset($this->jsonBody->errors) + ) { + require_once 'Zend/Service/Twitter/Exception.php'; + throw new Zend_Service_Twitter_Exception( + 'Either no JSON response received, or JSON error response is malformed; cannot return errors' + ); + } + return $this->jsonBody->errors; + } + + /** + * Retrieve the raw response body + * + * @return string + */ + public function getRawResponse() + { + return $this->rawBody; + } + + /** + * Retun the decoded response body + * + * @return array|stdClass + */ + public function toValue() + { + return $this->jsonBody; + } +}