@@ -16,6 +16,10 @@ class TwitterOAuth
16
16
{
17
17
protected $ url = 'https://api.twitter.com/1.1/ ' ;
18
18
19
+ protected $ outputFormats = array ('text ' , 'json ' , 'array ' , 'object ' );
20
+
21
+ protected $ defaultFormat = 'object ' ;
22
+
19
23
protected $ config = array ();
20
24
21
25
protected $ call = '' ;
@@ -26,8 +30,6 @@ class TwitterOAuth
26
30
27
31
protected $ postParams = array ();
28
32
29
- protected $ arrayOutput = false ;
30
-
31
33
32
34
/**
33
35
* Prepare a new conection with Twitter API via OAuth
@@ -36,42 +38,41 @@ class TwitterOAuth
36
38
*/
37
39
public function __construct (array $ config )
38
40
{
39
- $ keys = array (
41
+ $ required = array (
40
42
'consumer_key ' => '' ,
41
43
'consumer_secret ' => '' ,
42
44
'oauth_token ' => '' ,
43
45
'oauth_token_secret ' => ''
44
46
);
45
47
46
- if (count (array_intersect_key ($ keys , $ config )) !== count ($ keys )) {
48
+ if (count (array_intersect_key ($ required , $ config )) !== count ($ required )) {
47
49
throw new \Exception ('Missing parameters in configuration array ' );
48
50
}
49
51
52
+ if (!isset ($ config ['output_format ' ]) || !in_array ($ config ['output_format ' ], $ this ->outputFormats )) {
53
+ $ config ['output_format ' ] = $ this ->defaultFormat ;
54
+ }
55
+
50
56
$ this ->config = $ config ;
51
57
52
- unset($ keys , $ config );
58
+ unset($ required , $ config );
53
59
}
54
60
55
61
/**
56
62
* Send a GET call to Twitter API via OAuth
57
63
*
58
64
* @param string $call Twitter resource string
59
65
* @param array $getParams GET parameters to send
60
- * @param bool $arrayOutput Output format (false = Object | true = Array)
61
66
* @return mixed Output with selected format
62
67
*/
63
- public function get ($ call , array $ getParams = null , $ arrayOutput = false )
68
+ public function get ($ call , array $ getParams = null )
64
69
{
65
70
$ this ->call = $ call ;
66
71
67
72
if ($ getParams !== null && is_array ($ getParams )) {
68
73
$ this ->getParams = $ getParams ;
69
74
}
70
75
71
- if ($ arrayOutput !== false ) {
72
- $ this ->arrayOutput = true ;
73
- }
74
-
75
76
return $ this ->sendRequest ();
76
77
}
77
78
@@ -81,10 +82,9 @@ public function get($call, array $getParams = null, $arrayOutput = false)
81
82
* @param string $call Twitter resource string
82
83
* @param array $postParams POST parameters to send
83
84
* @param array $getParams GET parameters to send
84
- * @param bool $arrayOutput Output format (false = Object | true = Array)
85
85
* @return mixed Output with selected format
86
86
*/
87
- public function post ($ call , array $ postParams = null , array $ getParams = null , $ arrayOutput = false )
87
+ public function post ($ call , array $ postParams = null , array $ getParams = null )
88
88
{
89
89
$ this ->call = $ call ;
90
90
@@ -98,10 +98,6 @@ public function post($call, array $postParams = null, array $getParams = null, $
98
98
$ this ->getParams = $ getParams ;
99
99
}
100
100
101
- if ($ arrayOutput !== false ) {
102
- $ this ->arrayOutput = true ;
103
- }
104
-
105
101
return $ this ->sendRequest ();
106
102
}
107
103
@@ -251,6 +247,89 @@ protected function buildRequestHeader()
251
247
);
252
248
}
253
249
250
+ /**
251
+ * Processing Twitter Exceptions in case of error
252
+ *
253
+ * @param string $type Depends of response format (array|object)
254
+ * @param mixed $ex Exceptions
255
+ * @throws Exception\TwitterException
256
+ */
257
+ protected function processExceptions ($ type , $ ex )
258
+ {
259
+ switch ($ type ) {
260
+ case 'array ' :
261
+ foreach ($ ex ['errors ' ] as $ error ) {
262
+ throw new TwitterException ($ error ['message ' ], $ error ['code ' ]);
263
+ }
264
+
265
+ break ;
266
+
267
+ default :
268
+ foreach ($ ex ->errors as $ error ) {
269
+ throw new TwitterException ($ error ->message , $ error ->code );
270
+ }
271
+ }
272
+
273
+ unset($ tupe , $ ex , $ error );
274
+ }
275
+
276
+ /**
277
+ * Outputs the response in the selected format
278
+ *
279
+ * @param string $response
280
+ * @return mixed
281
+ */
282
+ protected function processOutput ($ response )
283
+ {
284
+ $ format = $ this ->config ['output_format ' ];
285
+
286
+ switch ($ format ) {
287
+ case 'text ' :
288
+ if (substr ($ response , 2 , 6 ) == 'errors ' ) {
289
+ $ response = json_decode ($ response );
290
+
291
+ $ this ->processExceptions ('object ' , $ response );
292
+ }
293
+
294
+ break ;
295
+
296
+ case 'json ' :
297
+ if (!headers_sent ()) {
298
+ header ('Cache-Control: no-cache, must-revalidate ' );
299
+ header ('Expires: Tue, 19 May 1981 00:00:00 GMT ' );
300
+ header ('Content-type: application/json ' );
301
+ }
302
+
303
+ if (substr ($ response , 2 , 6 ) == 'errors ' ) {
304
+ $ response = json_decode ($ response );
305
+
306
+ $ this ->processExceptions ('object ' , $ response );
307
+ }
308
+
309
+ break ;
310
+
311
+ case 'array ' :
312
+ $ response = json_decode ($ response , true );
313
+
314
+ if (isset ($ response ['errors ' ])) {
315
+ $ this ->processExceptions ('array ' , $ response );
316
+ }
317
+
318
+ break ;
319
+
320
+ default :
321
+ $ response = json_decode ($ response );
322
+
323
+ if (isset ($ response ->errors )) {
324
+ $ this ->processExceptions ('object ' , $ response );
325
+ }
326
+ }
327
+
328
+ unset($ format );
329
+
330
+ return $ response ;
331
+ }
332
+
254
333
/**
255
334
* Send GET or POST requests to Twitter API
256
335
*
@@ -290,18 +369,6 @@ protected function sendRequest()
290
369
throw new TwitterException (str_replace (array ("\n" , "\r" , "\t" ), '' , strip_tags ($ response )), 0 );
291
370
}
292
371
293
- $ response = json_decode ($ response , $ this ->arrayOutput );
294
-
295
- if (!$ this ->arrayOutput && isset ($ response ->errors )) {
296
- foreach ($ response ->errors as $ error ) {
297
- throw new TwitterException ($ error ->message , $ error ->code );
298
- }
299
- } elseif ($ this ->arrayOutput && isset ($ response ['errors ' ])) {
300
- foreach ($ response ['errors ' ] as $ error ) {
301
- throw new TwitterException ($ error ['message ' ], $ error ['code ' ]);
302
- }
303
- }
304
-
305
- return $ response ;
372
+ return $ this ->processOutput ($ response );
306
373
}
307
374
}
0 commit comments