Skip to content

Commit 080a03b

Browse files
committed
Add json_decode wrapper, see #151
1 parent 95eeea0 commit 080a03b

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ codebird-php - changelog
66
+ #152 Throw Exception on failed remote media download
77
+ Add REST API POST statuses/unretweet/:id
88
+ Add Ads API GET insights/keywords/search
9+
+ #151 Avoid JSON_BIGINT_AS_STRING errors
910

1011
3.0.0 (2016-01-01)
1112
+ Add unit testing suite

src/codebird.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,24 @@ protected function _mapFnInlineParams($method, &$apiparams)
996996
return [$method, $method_template];
997997
}
998998

999+
/**
1000+
* Avoids any JSON_BIGINT_AS_STRING errors
1001+
*
1002+
* @param string $data JSON data to decode
1003+
* @param int optional $need_array Decode as array, otherwise as object
1004+
*
1005+
* @return array|object The decoded object
1006+
*/
1007+
protected function _json_decode($data, $need_array = false)
1008+
{
1009+
if (!(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
1010+
return json_decode($data, $need_array, 512, JSON_BIGINT_AS_STRING);
1011+
}
1012+
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
1013+
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $data);
1014+
$obj = json_decode($json_without_bigints, $need_array);
1015+
return $obj;
1016+
}
9991017

10001018
/**
10011019
* Uncommon API methods
@@ -1336,7 +1354,7 @@ protected function _parseBearerReply($result, $httpstatus)
13361354
break;
13371355
case CODEBIRD_RETURNFORMAT_JSON:
13381356
if ($httpstatus === 200) {
1339-
$parsed = json_decode($reply, false, 512, JSON_BIGINT_AS_STRING);
1357+
$parsed = $this->_json_decode($reply);
13401358
self::setBearerToken($parsed->access_token);
13411359
}
13421360
break;
@@ -2232,7 +2250,7 @@ protected function _appendHttpStatusAndRate($reply, $httpstatus, $rate)
22322250
$reply->rate = $rate;
22332251
break;
22342252
case CODEBIRD_RETURNFORMAT_JSON:
2235-
$reply = json_decode($reply);
2253+
$reply = $this->_json_decode($reply);
22362254
$reply->httpstatus = $httpstatus;
22372255
$reply->rate = $rate;
22382256
$reply = json_encode($reply);
@@ -2566,7 +2584,7 @@ protected function _parseApiReply($reply)
25662584
return new \stdClass;
25672585
}
25682586
}
2569-
if (! $parsed = json_decode($reply, $need_array, 512, JSON_BIGINT_AS_STRING)) {
2587+
if (! $parsed = $this->_json_decode($reply, $need_array)) {
25702588
if ($reply) {
25712589
// assume query format
25722590
$reply = explode('&', $reply);

test/requestparse_tests.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,23 @@ public function testMapFnInlineParams()
190190
$this->assertArrayNotHasKey('resumeId', $apiparams);
191191
$this->assertEquals(['test' => 1], $apiparams);
192192
}
193+
194+
/**
195+
* Tests _json_decode
196+
*/
197+
public function testJsonDecode()
198+
{
199+
$json = '{"id": 123456789123456789, "id_str": "123456789123456789"}';
200+
$array = [
201+
'id' => 123456789123456789,
202+
'id_str' => '123456789123456789'
203+
];
204+
$object = (object) $array;
205+
206+
$cb = $this->getCB();
207+
$result = $cb->call('_json_decode', [$json]);
208+
$this->assertEquals($object, $result);
209+
$result = $cb->call('_json_decode', [$json, true]);
210+
$this->assertEquals($array, $result);
211+
}
193212
}

0 commit comments

Comments
 (0)