File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -89,6 +89,9 @@ class ClientBuilder
89
89
/** @var null|bool|string */
90
90
private $ sslVerification = null ;
91
91
92
+ /** @var bool */
93
+ private $ allowBadJSON = false ;
94
+
92
95
/**
93
96
* @return ClientBuilder
94
97
*/
@@ -395,11 +398,23 @@ public function setSSLVerification($value = true)
395
398
return $ this ;
396
399
}
397
400
401
+ public function allowBadJSONSerialization ()
402
+ {
403
+ $ this ->allowBadJSON = true ;
404
+ }
405
+
398
406
/**
399
407
* @return Client
400
408
*/
401
409
public function build ()
402
410
{
411
+ if (!defined ('JSON_PRESERVE_ZERO_FRACTION ' ) && $ this ->allowBadJSON === false ) {
412
+ throw new RuntimeException ("Your version of PHP / json-ext does not support the constant 'JSON_PRESERVE_ZERO_FRACTION', " .
413
+ " which is important for proper type mapping in Elasticsearch. Please upgrade your PHP or json-ext. \n" .
414
+ "If you are unable to upgrade, and are willing to accept the consequences, you may use the allowBadJSONSerialization() " .
415
+ " method on the ClientBuilder to bypass this limitation. " );
416
+ }
417
+
403
418
$ this ->buildLoggers ();
404
419
405
420
if (is_null ($ this ->handler )) {
Original file line number Diff line number Diff line change 15
15
*/
16
16
class SmartSerializer implements SerializerInterface
17
17
{
18
+ private $ PHP_VERSION ;
19
+
20
+ public function __construct ()
21
+ {
22
+ $ this ->PHP_VERSION = phpversion ();
23
+ }
24
+
18
25
/**
19
26
* Serialize assoc array into JSON string
20
27
*
@@ -27,7 +34,11 @@ public function serialize($data)
27
34
if (is_string ($ data ) === true ) {
28
35
return $ data ;
29
36
} else {
30
- $ data = json_encode ($ data , JSON_PRESERVE_ZERO_FRACTION );
37
+ if (version_compare ($ this ->PHP_VERSION , '5.6.6 ' , '< ' ) || ! defined ('JSON_PRESERVE_ZERO_FRACTION ' )) {
38
+ $ data = json_encode ($ data );
39
+ } else {
40
+ $ data = json_encode ($ data , JSON_PRESERVE_ZERO_FRACTION );
41
+ }
31
42
if ($ data === '[] ' ) {
32
43
return '{} ' ;
33
44
} else {
You can’t perform that action at this time.
0 commit comments