@@ -17,6 +17,7 @@ class FloatEncoder implements Encoder
17
17
private static $ defaultOptions = [
18
18
'float.integers ' => false ,
19
19
'float.precision ' => 17 ,
20
+ 'float.export ' => false ,
20
21
];
21
22
22
23
public function getDefaultOptions ()
@@ -53,15 +54,11 @@ private function encodeNumber($float, array $options, callable $encode)
53
54
return $ this ->encodeInteger ($ float , $ encode );
54
55
} elseif ($ float === 0.0 ) {
55
56
return '0.0 ' ;
57
+ } elseif ($ options ['float.export ' ]) {
58
+ return var_export ($ float , true );
56
59
}
57
60
58
- $ precision = $ options ['float.precision ' ];
59
-
60
- if ($ precision === false ) {
61
- $ precision = ini_get ('serialize_precision ' );
62
- }
63
-
64
- return $ this ->encodeFloat ($ float , $ precision );
61
+ return $ this ->encodeFloat ($ float , $ this ->determinePrecision ($ options ));
65
62
}
66
63
67
64
/**
@@ -98,6 +95,22 @@ private function encodeInteger($float, callable $encode)
98
95
return number_format ($ float , 0 , '. ' , '' );
99
96
}
100
97
98
+ /**
99
+ * Determines the float precision based on the options.
100
+ * @param array $options The float encoding options
101
+ * @return int The precision used to encode floats
102
+ */
103
+ private function determinePrecision ($ options )
104
+ {
105
+ $ precision = $ options ['float.precision ' ];
106
+
107
+ if ($ precision === false ) {
108
+ $ precision = defined ('HHVM_VERSION ' ) ? 17 : ini_get ('serialize_precision ' );
109
+ }
110
+
111
+ return max (1 , (int ) $ precision );
112
+ }
113
+
101
114
/**
102
115
* Encodes the number using a floating point representation.
103
116
* @param float $float The number to encode
@@ -106,7 +119,6 @@ private function encodeInteger($float, callable $encode)
106
119
*/
107
120
private function encodeFloat ($ float , $ precision )
108
121
{
109
- $ precision = max (1 , (int ) $ precision );
110
122
$ log = (int ) floor (log (abs ($ float ), 10 ));
111
123
112
124
if (abs ($ float ) < self ::FLOAT_MAX && $ log > -5 && abs ($ log ) < $ precision ) {
0 commit comments