Skip to content

Commit 36cd6ce

Browse files
committed
Merge branch 'master' of github.com:romanpitak/dotMailer-API-v2-PHP-client
2 parents 4191223 + cfb4508 commit 36cd6ce

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/DataTypes/MagicArray.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct($value = null)
2323
if (!is_string($value)) { // convert to string
2424
$value = (string)$value;
2525
}
26-
$value = json_decode($value, true, 512, JSON_BIGINT_AS_STRING);
26+
$value = $this->json_decode_bigint_as_string($value, true, 512);
2727
}
2828
// assign
2929
if (!is_null($value)) {
@@ -34,6 +34,29 @@ public function __construct($value = null)
3434
}
3535
}
3636

37+
/**
38+
*
39+
* In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you
40+
* to specify that large ints should be treated as strings, rather than the
41+
* PHP default behaviour of converting them to floats.
42+
*
43+
* Not all servers will support that, however, so for older versions we must
44+
* manually detect large ints in the JSON string and quote them (thus converting
45+
* them to strings) before decoding, hence the preg_replace() call.
46+
*
47+
* @link http://stackoverflow.com/a/27909889/1255333
48+
* @param $input
49+
*/
50+
private function json_decode_bigint_as_string($json, $assoc = false, $depth = 512) {
51+
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
52+
return json_decode($json, $assoc, $depth);
53+
} else {
54+
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
55+
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $json);
56+
return json_decode($json_without_bigints, $assoc, $depth);
57+
}
58+
}
59+
3760
/**
3861
* @param $offset
3962
* @throws InvalidOffsetException

0 commit comments

Comments
 (0)