@@ -465,34 +465,102 @@ public function getApiMethods()
465
465
public function __call ($ fn , $ params )
466
466
{
467
467
// 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
+ *
8000
td>510
+ * @param array|string $params Parameters to parse
511
+ *
512
+ * @return array $apiparams
513
+ */
514
+ protected function _parseApiParams ($ params )
515
+ {
468
516
$ 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 );
475
544
} 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 );
490
546
}
491
547
}
492
548
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
+ {
494
561
foreach ($ apiparams as $ key => $ value ) {
495
562
if (! is_scalar ($ value )) {
563
+ // no need to try replacing arrays
496
564
continue ;
497
565
}
498
566
if (is_null ($ value )) {
@@ -502,34 +570,24 @@ public function __call($fn, $params)
502
570
}
503
571
}
504
572
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
+ }
517
575
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
+ {
518
586
// 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
+
526
589
// 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 );
533
591
534
592
// replace AA by URL parameters
535
593
$ method_template = $ method ;
@@ -558,20 +616,44 @@ public function __call($fn, $params)
558
616
$ method_template = str_replace (chr (65 + $ i ), '_ ' . chr (97 + $ i ), $ method_template );
559
617
}
560
618
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
+ }
564
621
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 ;
573
635
}
574
636
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
+
575
657
/**
576
658
* Uncommon API methods
577
659
*/
0 commit comments