Skip to content

Commit

Permalink
Add json_decode wrapper, see #151
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetx committed Jan 23, 2016
1 parent 95eeea0 commit 080a03b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ codebird-php - changelog
+ #152 Throw Exception on failed remote media download
+ Add REST API POST statuses/unretweet/:id
+ Add Ads API GET insights/keywords/search
+ #151 Avoid JSON_BIGINT_AS_STRING errors

3.0.0 (2016-01-01)
+ Add unit testing suite
Expand Down
24 changes: 21 additions & 3 deletions src/codebird.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,24 @@ protected function _mapFnInlineParams($method, &$apiparams)
return [$method, $method_template];
}

/**
* Avoids any JSON_BIGINT_AS_STRING errors
*
* @param string $data JSON data to decode
* @param int optional $need_array Decode as array, otherwise as object
*
* @return array|object The decoded object
*/
protected function _json_decode($data, $need_array = false)
{
if (!(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
return json_decode($data, $need_array, 512, JSON_BIGINT_AS_STRING);
}
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $data);
$obj = json_decode($json_without_bigints, $need_array);
return $obj;
}

/**
* Uncommon API methods
Expand Down Expand Up @@ -1336,7 +1354,7 @@ protected function _parseBearerReply($result, $httpstatus)
break;
case CODEBIRD_RETURNFORMAT_JSON:
if ($httpstatus === 200) {
$parsed = json_decode($reply, false, 512, JSON_BIGINT_AS_STRING);
$parsed = $this->_json_decode($reply);
self::setBearerToken($parsed->access_token);
}
break;
Expand Down Expand Up @@ -2232,7 +2250,7 @@ protected function _appendHttpStatusAndRate($reply, $httpstatus, $rate)
$reply->rate = $rate;
break;
case CODEBIRD_RETURNFORMAT_JSON:
$reply = json_decode($reply);
$reply = $this->_json_decode($reply);
$reply->httpstatus = $httpstatus;
$reply->rate = $rate;
$reply = json_encode($reply);
Expand Down Expand Up @@ -2566,7 +2584,7 @@ protected function _parseApiReply($reply)
return new \stdClass;
}
}
if (! $parsed = json_decode($reply, $need_array, 512, JSON_BIGINT_AS_STRING)) {
if (! $parsed = $this->_json_decode($reply, $need_array)) {
if ($reply) {
// assume query format
$reply = explode('&', $reply);
Expand Down
19 changes: 19 additions & 0 deletions test/requestparse_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,23 @@ public function testMapFnInlineParams()
$this->assertArrayNotHasKey('resumeId', $apiparams);
$this->assertEquals(['test' => 1], $apiparams);
}

/**
* Tests _json_decode
*/
public function testJsonDecode()
{
$json = '{"id": 123456789123456789, "id_str": "123456789123456789"}';
$array = [
'id' => 123456789123456789,
'id_str' => '123456789123456789'
];
$object = (object) $array;

$cb = $this->getCB();
$result = $cb->call('_json_decode', [$json]);
$this->assertEquals($object, $result);
$result = $cb->call('_json_decode', [$json, true]);
$this->assertEquals($array, $result);
}
}

0 comments on commit 080a03b

Please sign in to comment.