Skip to content

Commit

Permalink
Merge branch 'master' of github.com:romanpitak/dotMailer-API-v2-PHP-c…
Browse files Browse the repository at this point in the history
…lient
  • Loading branch information
romanpitak committed Jul 1, 2016
2 parents 4191223 + cfb4508 commit 36cd6ce
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/DataTypes/MagicArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct($value = null)
if (!is_string($value)) { // convert to string
$value = (string)$value;
}
$value = json_decode($value, true, 512, JSON_BIGINT_AS_STRING);
$value = $this->json_decode_bigint_as_string($value, true, 512);
}
// assign
if (!is_null($value)) {
Expand All @@ -34,6 +34,29 @@ public function __construct($value = null)
}
}

/**
*
* In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you
* to specify that large ints should be treated as strings, rather than the
* PHP default behaviour of converting them to floats.
*
* Not all servers will support that, however, so for older versions we must
* manually detect large ints in the JSON string and quote them (thus converting
* them to strings) before decoding, hence the preg_replace() call.
*
* @link http://stackoverflow.com/a/27909889/1255333
* @param $input
*/
private function json_decode_bigint_as_string($json, $assoc = false, $depth = 512) {
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
return json_decode($json, $assoc, $depth);
} else {
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $json);
return json_decode($json_without_bigints, $assoc, $depth);
}
}

/**
* @param $offset
* @throws InvalidOffsetException
Expand Down

0 comments on commit 36cd6ce

Please sign in to comment.