Version 1
[yaffs-website] / vendor / php-instagram-api / php-instagram-api / Instagram / Net / ApiResponse.php
1 <?php
2
3 /**
4 * Instagram PHP
5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
7 */
8
9 namespace Instagram\Net;
10
11 /**
12  * API Response
13  *
14  * Holds the API response
15  */
16 class ApiResponse {
17
18     /**
19      * Response
20      *
21      * This is the response from the API
22      *
23      * @var StdClass
24      * @access protected
25      */
26     protected $response;
27
28     /**
29      * Constructor
30      *
31      * @param $raw_response Response from teh API
32      * @access public
33      */
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';
42         }
43     }
44
45     /**
46      * Is Valid
47      *
48      * Returns true if the API returned an error, otherwise false
49      *
50      * @return boolean
51      * @access public
52      */
53     public function isValid() {
54         return
55             $this->response instanceof \StdClass &&
56             !isset( $this->response->meta->error_type ) &&
57             !isset( $this->response->error_type );
58     }
59     /**
60      * Is Valid API Response
61      *
62      * Returns true if the response was a valid response from the API, otherwise false
63      *
64      * @return boolean
65      * @access public
66      */
67     public function isValidApiResponse() {
68         return $this->response instanceof \StdClass;
69     }
70
71     /**
72      * Get the response data
73      *
74      * @return mixed Return the response's data or null
75      * @access public
76      */
77     public function getData() {
78         return isset( $this->response->data ) ? $this->response->data : null;
79     }
80
81     /**
82      *  Get the raw response
83      *
84      * @return mixed Returns the response or null
85      * @access public
86      */
87     public function getRawData() {
88         return isset( $this->response ) ? $this->response : null;
89     }
90
91
92
93     /**
94      * Get the response's error message
95      *
96      * @return mixed Returns the error message or null
97      * @access public
98      */
99     public function getErrorMessage() {
100         if ( isset( $this->response->error_message ) ) {
101             return $this->response->error_message;
102         }
103         if( isset( $this->response->meta->error_message ) ) {
104             return $this->response->meta->error_message;
105         }
106         return null;
107     }
108
109     /**
110      * Get the error code
111      *
112      * @return mixed Returns the error code or null
113      * @access public
114      */
115     public function getErrorCode() {
116         if ( isset( $this->response->code ) ) {
117             return $this->response->code;
118         }
119         if( isset( $this->response->meta->code ) ) {
120             return $this->response->meta->code;
121         }
122         return null;
123     }
124
125     /**
126      * Get the error type
127      * 
128      * @return mixed Returns the error type or null
129      * @access public
130      */
131     public function getErrorType() {
132         if ( isset( $this->response->error_type ) ) {
133             return $this->response->error_type;
134         }
135         if( isset( $this->response->meta->error_type ) ) {
136             return $this->response->meta->error_type;
137         }
138         return null;
139     }
140
141     /**
142      * Magic to string method
143      *
144      * @return string Return the json encoded response
145      * @access public
146      */
147     public function __toString() {
148         return json_encode( $this->response );
149     }
150
151 }