Skip to content

Commit ebd5cf9

Browse files
committed
Added Multi Output Formats (text|json|array|object)
1 parent 6b7e1ba commit ebd5cf9

File tree

1 file changed

+97
-30
lines changed

1 file changed

+97
-30
lines changed

TwitterOAuth/TwitterOAuth.php

Lines changed: 97 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class TwitterOAuth
1616
{
1717
protected $url = 'https://api.twitter.com/1.1/';
1818

19+
protected $outputFormats = array('text', 'json', 'array', 'object');
20+
21+
protected $defaultFormat = 'object';
22+
1923
protected $config = array();
2024

2125
protected $call = '';
@@ -26,8 +30,6 @@ class TwitterOAuth
2630

2731
protected $postParams = array();
2832

29-
protected $arrayOutput = false;
30-
3133

3234
/**
3335
* Prepare a new conection with Twitter API via OAuth
@@ -36,42 +38,41 @@ class TwitterOAuth
3638
*/
3739
public function __construct(array $config)
3840
{
39-
$keys = array(
41+
$required = array(
4042
'consumer_key' => '',
4143
'consumer_secret' => '',
4244
'oauth_token' => '',
4345
'oauth_token_secret' => ''
4446
);
4547

46-
if (count(array_intersect_key($keys, $config)) !== count($keys)) {
48+
if (count(array_intersect_key($required, $config)) !== count($required)) {
4749
throw new \Exception('Missing parameters in configuration array');
4850
}
4951

52+
if (!isset($config['output_format']) || !in_array($config['output_format'], $this->outputFormats)) {
53+
$config['output_format'] = $this->defaultFormat;
54+
}
55+
5056
$this->config = $config;
5157

52-
unset($keys, $config);
58+
unset($required, $config);
5359
}
5460

5561
/**
5662
* Send a GET call to Twitter API via OAuth
5763
*
5864
* @param string $call Twitter resource string
5965
* @param array $getParams GET parameters to send
60-
* @param bool $arrayOutput Output format (false = Object | true = Array)
6166
* @return mixed Output with selected format
6267
*/
63-
public function get($call, array $getParams = null, $arrayOutput = false)
68+
public function get($call, array $getParams = null)
6469
{
6570
$this->call = $call;
6671

6772
if ($getParams !== null && is_array($getParams)) {
6873
$this->getParams = $getParams;
6974
}
7075

71-
if ($arrayOutput !== false) {
72-
$this->arrayOutput = true;
73-
}
74-
7576
return $this->sendRequest();
7677
}
7778

@@ -81,10 +82,9 @@ public function get($call, array $getParams = null, $arrayOutput = false)
8182
* @param string $call Twitter resource string
8283
* @param array $postParams POST parameters to send
8384
* @param array $getParams GET parameters to send
84-
* @param bool $arrayOutput Output format (false = Object | true = Array)
8585
* @return mixed Output with selected format
8686
*/
87-
public function post($call, array $postParams = null, array $getParams = null, $arrayOutput = false)
87+
public function post($call, array $postParams = null, array $getParams = null)
8888
{
8989
$this->call = $call;
9090

@@ -98,10 +98,6 @@ public function post($call, array $postParams = null, array $getParams = null, $
9898
$this->getParams = $getParams;
9999
}
100100

101-
if ($arrayOutput !== false) {
102-
$this->arrayOutput = true;
103-
}
104-
105101
return $this->sendRequest();
106102
}
107103

@@ -251,6 +247,89 @@ protected function buildRequestHeader()
251247
);
252248
}
253249

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+
254333
/**
255334
* Send GET or POST requests to Twitter API
256335
*
@@ -290,18 +369,6 @@ protected function sendRequest()
290369
throw new TwitterException(str_replace(array("\n", "\r", "\t"), '', strip_tags($response)), 0);
291370
}
292371

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);
306373
}
307374
}

0 commit comments

Comments
 (0)