1 | <?php |
||
22 | class Eventbrite |
||
23 | { |
||
24 | /** |
||
25 | * The current version of this library. |
||
26 | */ |
||
27 | const VERSION = '0.1'; |
||
28 | |||
29 | /** |
||
30 | * An array of valid HTTP verbs. |
||
31 | */ |
||
32 | CONST VALID_VERBS = ['get', 'post', 'put', 'patch', 'delete']; |
||
33 | |||
34 | /** |
||
35 | * The API endpoint to get the current user's details. |
||
36 | */ |
||
37 | const CURRENT_USER_ENDPOINT = 'users/me/'; |
||
38 | |||
39 | /** |
||
40 | * @var string The Eventbrite OAuth token. |
||
41 | */ |
||
42 | private $token; |
||
43 | |||
44 | /** |
||
45 | * @var \GuzzleHttp\Client |
||
46 | */ |
||
47 | private $client; |
||
48 | |||
49 | /** |
||
50 | * @var \Psr\Http\Message\ResponseInterface |
||
51 | */ |
||
52 | private $last_response = null; |
||
53 | |||
54 | /** |
||
55 | * @param string $token |
||
56 | * The OAuth token to authenticate the request |
||
57 | * @param array $config |
||
58 | * An array of Guzzle config options so that everything is configurable. |
||
59 | * |
||
60 | * @throws \Exception |
||
61 | */ |
||
62 | public function __construct($token, $config = []) |
||
82 | |||
83 | /** |
||
84 | * Make the call to Eventbrite, only synchronous calls at present. |
||
85 | * |
||
86 | * @param string $verb |
||
87 | * @param string $endpoint |
||
88 | * @param array $options |
||
89 | * |
||
90 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | public function call($verb, $endpoint, $options = []) |
||
122 | |||
123 | /** |
||
124 | * A slightly abstracted wrapper around call(). |
||
125 | * |
||
126 | * This essentially splits the call options array into different parameters |
||
127 | * to make it more obvious to less advanced users what parameters can be |
||
128 | * passed to the client. |
||
129 | * |
||
130 | * @param $verb |
||
131 | * @param $endpoint |
||
132 | * @param null $params |
||
133 | * @param null $body |
||
134 | * @param null $headers |
||
135 | * @param array $options |
||
136 | * |
||
137 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
138 | * @throws \Exception |
||
139 | */ |
||
140 | public function makeRequest($verb, $endpoint, $params = null, $body = null, $headers = null, $options = []) |
||
162 | |||
163 | /** |
||
164 | * Checks if the HTTP method being used is correct. |
||
165 | * |
||
166 | * @param $http_method |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | public function validMethod($http_method) |
||
177 | |||
178 | /** |
||
179 | * Parses the response from |
||
180 | * |
||
181 | * @param \Psr\Http\Message\ResponseInterface $response |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | private function parseResponse(ResponseInterface $response) |
||
195 | |||
196 | /** |
||
197 | * Checks a string to see if it's JSON. True if it is, false if it's not. |
||
198 | * |
||
199 | * @param string $string |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function isValidJson($string) |
||
211 | |||
212 | /** |
||
213 | * Checks if the class can connect to the Eventbrite API. |
||
214 | * |
||
215 | * Checks if we can connect to the API by calling the user endpoint and |
||
216 | * checking the response code. If the response code is 2xx it returns true, |
||
217 | * otherwise false. |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function canConnect() |
||
229 | |||
230 | /** |
||
231 | * Provides shortcut methods named by HTTP verbs. |
||
232 | * |
||
233 | * Provides shortcut methods for GET, POST, PUT, PATCH and DELETE. |
||
234 | * |
||
235 | * @param string $method |
||
236 | * @param array $args |
||
237 | * |
||
238 | * @return array|mixed|\Psr\Http\Message\ResponseInterface |
||
239 | * @throws \Exception |
||
240 | */ |
||
241 | public function __call($method, $args) { |
||
250 | |||
251 | /** |
||
252 | * Returns the last response object for inspection. |
||
253 | * |
||
254 | * @return \Psr\Http\Message\ResponseInterface |
||
255 | */ |
||
256 | public function getLastResponse() |
||
260 | |||
261 | } |
||
262 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: