5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
9 namespace Instagram\Net;
14 * Holds the API response
21 * This is the response from the API
31 * @param $raw_response Response from teh API
34 public function __construct( $raw_response ){
35 $this->response = json_decode( $raw_response );
36 if ( !$this->isValidApiResponse() ) {
37 $this->response = new \StdClass;
38 $this->response->meta = new \StdClass;
39 $this->response->meta->error_type = 'UnknownAPiError';
40 $this->response->meta->code = 555;
41 $this->response->meta->error_message = 'Unknown error';
48 * Returns true if the API returned an error, otherwise false
53 public function isValid() {
55 $this->response instanceof \StdClass &&
56 !isset( $this->response->meta->error_type ) &&
57 !isset( $this->response->error_type );
60 * Is Valid API Response
62 * Returns true if the response was a valid response from the API, otherwise false
67 public function isValidApiResponse() {
68 return $this->response instanceof \StdClass;
72 * Get the response data
74 * @return mixed Return the response's data or null
77 public function getData() {
78 return isset( $this->response->data ) ? $this->response->data : null;
82 * Get the raw response
84 * @return mixed Returns the response or null
87 public function getRawData() {
88 return isset( $this->response ) ? $this->response : null;
94 * Get the response's error message
96 * @return mixed Returns the error message or null
99 public function getErrorMessage() {
100 if ( isset( $this->response->error_message ) ) {
101 return $this->response->error_message;
103 if( isset( $this->response->meta->error_message ) ) {
104 return $this->response->meta->error_message;
112 * @return mixed Returns the error code or null
115 public function getErrorCode() {
116 if ( isset( $this->response->code ) ) {
117 return $this->response->code;
119 if( isset( $this->response->meta->code ) ) {
120 return $this->response->meta->code;
128 * @return mixed Returns the error type or null
131 public function getErrorType() {
132 if ( isset( $this->response->error_type ) ) {
133 return $this->response->error_type;
135 if( isset( $this->response->meta->error_type ) ) {
136 return $this->response->meta->error_type;
142 * Magic to string method
144 * @return string Return the json encoded response
147 public function __toString() {
148 return json_encode( $this->response );