8000 Support API methods with templated variables (like statuses/show/:id) · codezninja/codebird-php@4b3255d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b3255d

Browse files
committed
Support API methods with templated variables (like statuses/show/:id)
1 parent 2cd29ff commit 4b3255d

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

codebird.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public function setReturnFormat($return_format)
7878

7979
public function __call($fn, $params)
8080
{
81+
// parse parameters
82+
$apiparams = array();
83+
if (count($params) > 0) {
84+
if (is_array($params[0])) {
85+
$apiparams = $params[0];
86+
} else {
87+
parse_str($params[0], $apiparams);
88+
}
89+
}
90+
8191
// map function name to API method
8292
$method = '';
8393

@@ -90,29 +100,31 @@ public function __call($fn, $params)
90100
$method .= $path[$i];
91101
}
92102

93-
// replace AA by {aa}
103+
// replace AA by URL parameters
104+
$method2 = $method;
94105
$match = array();
95106
if (preg_match('/[A-Z]{2,}/', $method, $match)) {
96-
print_r($match);
97-
die();
107+
foreach ($match as $param) {
108+
$param_l = strtolower($param);
109+
if (! isset($apiparams[$param_l])) {
110+
throw new Exception('To call the templated method "' . $method
111+
. '", specify the parameter value for "' . $param_l . '".');
112+
}
113+
$method = str_replace($param, $apiparams[$param_l], $method);
114+
$method2 = str_replace($param, ':' . $param_l, $method2);
115+
unset($apiparams[$param_l]);
116+
}
98117
}
99118

100119
// replace A-Z by _a-z
101120
for ($i = 0; $i < 26; $i++) {
102121
$method = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method);
122+
$method2 = str_replace(chr(65 + $i), '_' . chr(97 + $i), $method2);
103123
}
104-
$httpmethod = $this->_detectMethod($method);
105-
$sign = $this->_detectSign($method);
106-
$multipart = $this->_detectMultipart($method);
107124

108-
$apiparams = array();
109-
if (count($params) > 0) {
110-
if (is_array($params[0])) {
111-
$apiparams = $params[0];
112-
} else {
113-
parse_str($params[0], $apiparams);
114-
}
115-
}
125+
$httpmethod = $this->_detectMethod($method2);
126+
$sign = $this->_detectSign($method2);
127+
$multipart = $this->_detectMultipart($method2);
116128

117129
return $this->_callApi($httpmethod, $method, $apiparams, $sign, $multipart);
118130
}
@@ -339,7 +351,7 @@ private function _detectMethod($method)
339351
return $httpmethod;
340352
}
341353
}
342-
return null;
354+
throw new Exception('Can\'t find HTTP method to use for "' . $method . '".');
343355
}
344356

345357
/**

0 commit comments

Comments
 (0)
0