@@ -37,19 +37,20 @@ public function encode($value, $depth, array $options, callable $encode)
37
37
return $ value < 0 ? '-INF ' : 'INF ' ;
38
38
}
39
39
40
- return $ this ->encodeNumber ($ value , $ options );
40
+ return $ this ->encodeNumber ($ value , $ options, $ encode );
41
41
}
42
42
43
43
/**
44
44
* Encodes the number as a PHP number representation.
45
45
* @param float $float The number to encode
46
46
* @param array $options The float encoding options
47
+ * @param callable $encode Callback used to encode values
47
48
* @return string The PHP code representation for the number
48
49
*/
49
- private function encodeNumber ($ float , array $ options )
50
+ private function encodeNumber ($ float , array $ options, callable $ encode )
50
51
{
51
52
if ($ this ->isInteger ($ float , $ options ['float.integers ' ])) {
52
- return number_format ($ float , 0 , ' . ' , '' );
53
+ return $ this -> encodeInteger ($ float , $ encode );
53
54
} elseif ($ float === 0.0 ) {
54
55
return '0.0 ' ;
55
56
}
@@ -80,6 +81,23 @@ private function isInteger($float, $allowIntegers)
80
81
return $ allowIntegers === 'all ' ;
81
82
}
82
83
84
+ /**
85
+ * Encodes the given float as an integer.
86
+ * @param float $float The number to encode
87
+ * @param callable $encode Callback used to encode values
88
+ * @return string The PHP code representation for the number
89
+ */
90
+ private function encodeInteger ($ float , callable $ encode )
91
+ {
92
+ $ minimum = defined ('PHP_INT_MIN ' ) ? PHP_INT_MIN : ~PHP_INT_MAX ;
93
+
94
+ if ($ float >= $ minimum && $ float <= PHP_INT_MAX ) {
95
+ return $ encode ((int ) $ float );
96
+ }
97
+
98
+ return number_format ($ float , 0 , '. ' , '' );
99
+ }
100
+
83
101
/**
84
102
* Encodes the number using a floating point representation.
85
103
* @param float $float The number to encode
0 commit comments