8000 Refactor __call() method · SuperConnvrd/codebird-php@55b770f · GitHub
[go: up one dir, main page]

Skip to content

Commit 55b770f

Browse files
committed
Refactor __call() method
1 parent 0684f3b commit 55b770f

File tree

1 file changed

+139
-57
lines changed

1 file changed

+139
-57
lines changed

src/codebird.php

Lines changed: 139 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -465,34 +465,102 @@ public function getApiMethods()
465465
public function __call($fn, $params)
466466
{
467467
// parse parameters
468+
$apiparams = $this->_parseApiParams($params);
469+
470+
// stringify null and boolean parameters
471+
$apiparams = $this->_stringifyNullBoolParams($apiparams);
472+
473+
$app_only_auth = false;
474+
if (count($params) > 1) {
475+
// convert app_only_auth param to bool
476+
$app_only_auth = !! $params[1];
477+
}
478+
479+
// reset token when requesting a new token
480+
// (causes 401 for signature error on subsequent requests)
481+
if ($fn === 'oauth_requestToken') {
482+
$this->setToken(null, null);
483+
}
484+
485+
// map function name to API method
486+
list($method, $method_template) = $this->_mapFnToApiMethod($fn, $apiparams);
487+
488+
$httpmethod = $this->_detectMethod($method_template, $apiparams);
489+
$multipart = $this->_detectMultipart($method_template);
490+
$internal = $this->_detectInternal($method_template);
491+
492+
return $this->_callApi(
493+
$httpmethod,
494+
$method,
495+
$apiparams,
496+
$multipart,
497+
$app_only_auth,
498+
$internal
499+
);
500+
}
501+
502+
503+
/**
504+
* __call() helpers
505+
*/
506+
507+
/**
508+
* Parse given params, detect query-style params
509+
*
510+
* @param array|string $params Parameters to parse
511+
*
512+
* @return array $apiparams
513+
*/
514+
protected function _parseApiParams($params)
515+
{
468516
$apiparams = array();
469-
if (count($params) > 0) {
470-
if (is_array($params[0])) {
471-
$apiparams = $params[0];
472-
if (! is_array($apiparams)) {
473-
$apiparams = array();
474-
}
517+
if (count($params) === 0) {
518+
return $apiparams;
519+
}
520+
521+
if (is_array($params[0])) {
522+
// given parameters are array
523+
$apiparams = $params[0];
524+
if (! is_array($apiparams)) {
525+
$apiparams = array();
526+
}
527+
return $apiparams;
528+
}
529+
530+
// user gave us query-style params
531+
parse_str($params[0], $apiparams);
532+
if (! is_array($apiparams)) {
533+
$apiparams = array();
534+
}
535+
536+
if (! get_magic_quotes_gpc()) {
537+
return $apiparams;
538+
}
539+
540+
// remove auto-added slashes recursively if on magic quotes steroids
541+
foreach($apiparams as $key => $value) {
542+
if (is_array($value)) {
543+
$apiparams[$key] = array_map('stripslashes', $value);
475544
} else {
476-
parse_str($params[0], $apiparams);
477-
if (! is_array($apiparams)) {
478-
$apiparams = array();
479-
}
480-
// remove auto-added slashes if on magic quotes steroids
481-
if (get_magic_quotes_gpc()) {
482-
foreach($apiparams as $key => $value) {
483-
if (is_array($value)) {
484-
$apiparams[$key] = array_map('stripslashes', $value);
485-
} else {
486-
$apiparams[$key] = stripslashes($value);
487-
}
488-
}
489-
}
545+
$apiparams[$key] = stripslashes($value);
490546
}
491547
}
492548

493-
// stringify null and boolean parameters
549+
return $apiparams;
550+
}
551+
552+
/**
553+
* Replace null and boolean parameters with their string representations
554+
*
555+
* @param array $apiparams Parameter array to replace in
556+
*
557+
* @return array $apiparams
558+
*/
559+
protected function _stringifyNullBoolParams($apiparams)
560+
{
494561
foreach ($apiparams as $key => $value) {
495562
if (! is_scalar($value)) {
563+
// no need to try replacing arrays
496564
continue;
497565
}
498566
if (is_null($value)) {
@@ -502,34 +570,24 @@ public function __call($fn, $params)
502570
}
503571
}
504572

505-
$app_only_auth = false;
506-
if (count($params) > 1) {
507-
$app_only_auth = !! $params[1];
508-
}
509-
510-
// reset token when requesting a new token (causes 401 for signature error on 2nd+ requests)
511-
if ($fn === 'oauth_requestToken') {
512-
$this->setToken(null, null);
513-
}
514-
515-
// map function name to API method
516-
$method = '';
573+
return $apiparams;
574+
}
517575

576+
/**
577+
* Maps called PHP magic method name to Twitter API method
578+
*
579+
* @param string $fn Function called
580+
* @param array $apiparams byref API parameters
581+
*
582+
* @return array (string method, string method_template)
583+
*/
584+
protected function _mapFnToApiMethod($fn, &$apiparams)
585+
{
518586
// replace _ by /
519-
$path = explode('_', $fn);
520-
for ($i = 0; $i < count($path); $i++) {
521-
if ($i > 0) {
522-
$method .= '/';
523-
}
524-
$method .= $path[$i];
525-
}
587+
$method = $this->_mapFnInsertSlashes($fn);
588+
526589
// undo replacement for URL parameters
527-
$url_parameters_with_underscore = array('screen_name', 'place_id');
528-
foreach ($url_parameters_with_underscore as $param) {
529-
$param = strtoupper($param);
530-
$replacement_was = str_replace('_', '/', $param);
531-
$method = str_replace($replacement_was, $param, $method);
532-
}
590+
$method = $this->_mapFnRestoreParamUnderscores($method);
533591

534592
// replace AA by URL parameters
535593
$method_template = $method;
@@ -558,20 +616,44 @@ public function __call($fn, $params)
558616
$method_template = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method_template);
559617
}
560618

561-
$httpmethod = $this->_detectMethod($method_template, $apiparams);
562-
$multipart = $this->_detectMultipart($method_template);
563-
$internal = $this->_detectInternal($method_template);
619+
return array($method, $method_template);
620+
}
564621

565-
return $this->_callApi(
566-
$httpmethod,
567-
$method,
568-
$apiparams,
569-
$multipart,
570-
$app_only_auth,
571-
$internal
572-
);
622+
/**
623+
* API method mapping: Replaces _ with / character
624+
*
625+
* @param string $fn Function called
626+
*
627+
* @return string API method to call
628+
*/
629+
protected function _mapFnInsertSlashes($fn)
630+
{
631+
$path = explode('_', $fn);
632+
$method = implode('/', $path);
633+
634+
return $method;
573635
}
574636

637+
/**
638+
* API method mapping: Restore _ character in named parameters
639+
*
640+
* @param string $method API method to call
641+
*
642+
* @return string API method with restored underscores
643+
*/
644+
protected function _mapFnRestoreParamUnderscores($method)
645+
{
646+
$url_parameters_with_underscore = array('screen_name', 'place_id');
647+
foreach ($url_parameters_with_underscore as $param) {
648+
$param = strtoupper($param);
649+
$replacement_was = str_replace('_', '/', $param);
650+
$method = str_replace($replacement_was, $param, $method);
651+
}
652+
653+
return $method;
654+
}
655+
656+
575657
/**
576658
* Uncommon API methods
577659
*/

0 commit comments

Comments
 (0)
0