1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riimu\Kit\PHPEncoder\Encoder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Encoder for float values. |
7
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2014-2017 Riikka Kalliomäki |
9
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
10
|
|
|
*/ |
11
|
|
|
class FloatEncoder implements Encoder |
12
|
|
|
{ |
13
|
|
|
/** The maximum value that can be accurately represented by a float */ |
14
|
|
|
const FLOAT_MAX = 9007199254740992.0; |
15
|
|
|
|
16
|
|
|
/** @var array Default values for options in the encoder */ |
17
|
|
|
private static $defaultOptions = [ |
18
|
|
|
'float.integers' => false, |
19
|
|
|
'float.precision' => 17, |
20
|
|
|
'float.export' => false, |
21
|
|
|
]; |
22
|
|
|
|
23
|
165 |
|
public function getDefaultOptions() |
24
|
|
|
{ |
25
|
165 |
|
return self::$defaultOptions; |
26
|
|
|
} |
27
|
|
|
|
28
|
135 |
|
public function supports($value) |
29
|
|
|
{ |
30
|
135 |
|
return is_float($value); |
31
|
|
|
} |
32
|
|
|
|
33
|
36 |
|
public function encode($value, $depth, array $options, callable $encode) |
34
|
|
|
{ |
35
|
36 |
|
if (is_nan($value)) { |
36
|
3 |
|
return 'NAN'; |
37
|
33 |
|
} elseif (is_infinite($value)) { |
38
|
3 |
|
return $value < 0 ? '-INF' : 'INF'; |
39
|
|
|
} |
40
|
|
|
|
41
|
30 |
|
return $this->encodeNumber($value, $options, $encode); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Encodes the number as a PHP number representation. |
46
|
|
|
* @param float $float The number to encode |
47
|
|
|
* @param array $options The float encoding options |
48
|
|
|
* @param callable $encode Callback used to encode values |
49
|
|
|
* @return string The PHP code representation for the number |
50
|
|
|
*/ |
51
|
30 |
|
private function encodeNumber($float, array $options, callable $encode) |
52
|
|
|
{ |
53
|
30 |
|
if ($this->isInteger($float, $options['float.integers'])) { |
54
|
9 |
|
return $this->encodeInteger($float, $encode); |
55
|
27 |
|
} elseif ($float === 0.0) { |
56
|
3 |
|
return '0.0'; |
57
|
27 |
|
} elseif ($options['float.export']) { |
58
|
3 |
|
return var_export((float) $float, true); |
59
|
|
|
} |
60
|
|
|
|
61
|
24 |
|
return $this->encodeFloat($float, $this->determinePrecision($options)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Tells if the number can be encoded as an integer. |
66
|
|
|
* @param float $float The number to test |
67
|
|
|
* @param bool|string $allowIntegers Whether integers should be allowed |
68
|
|
|
* @return bool True if the number can be encoded as an integer, false if not |
69
|
|
|
*/ |
70
|
30 |
|
private function isInteger($float, $allowIntegers) |
71
|
|
|
{ |
72
|
30 |
|
if (!$allowIntegers || round($float) !== $float) { |
73
|
21 |
|
return false; |
74
|
9 |
|
} elseif (abs($float) < self::FLOAT_MAX) { |
75
|
6 |
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
return $allowIntegers === 'all'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Encodes the given float as an integer. |
83
|
|
|
* @param float $float The number to encode |
84
|
|
|
* @param callable $encode Callback used to encode values |
85
|
|
|
* @return string The PHP code representation for the number |
86
|
|
|
*/ |
87
|
9 |
|
private function encodeInteger($float, callable $encode) |
88
|
|
|
{ |
89
|
9 |
|
$minimum = defined('PHP_INT_MIN') ? PHP_INT_MIN : ~PHP_INT_MAX; |
90
|
|
|
|
91
|
9 |
|
if ($float >= $minimum && $float <= PHP_INT_MAX) { |
92
|
6 |
|
return $encode((int) $float); |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
return number_format($float, 0, '.', ''); |
96
|
|
|
} |
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
|
24 |
|
private function determinePrecision($options) |
104
|
|
|
{ |
105
|
24 |
|
$precision = $options['float.precision']; |
106
|
|
|
|
107
|
24 |
|
if ($precision === false) { |
108
|
3 |
|
$precision = defined('HHVM_VERSION') ? 17 : ini_get('serialize_precision'); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
24 |
|
return max(1, (int) $precision); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Encodes the number using a floating point representation. |
116
|
|
|
* @param float $float The number to encode |
117
|
|
|
* @param int $precision The maximum precision of encoded floats |
118
|
|
|
* @return string The PHP code representation for the number |
119
|
|
|
*/ |
120
|
24 |
|
private function encodeFloat($float, $precision) |
121
|
|
|
{ |
122
|
24 |
|
$log = (int) floor(log(abs($float), 10)); |
123
|
|
|
|
124
|
24 |
|
if ($log > -5 && abs($float) < self::FLOAT_MAX && abs($log) < $precision) { |
125
|
12 |
|
return $this->formatFloat($float, $precision - $log - 1); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Deal with overflow that results from rounding |
129
|
15 |
|
$log += (int) (round(abs($float) / 10 ** $log, $precision - 1) / 10); |
130
|
15 |
|
$string = $this->formatFloat($float / 10 ** $log, $precision - 1); |
131
|
|
|
|
132
|
15 |
|
return sprintf('%sE%+d', $string, $log); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Formats the number as a decimal number. |
137
|
|
|
* @param float $float The number to format |
138
|
|
|
* @param int $digits The maximum number of decimal digits |
139
|
|
|
* @return string The number formatted as a decimal number |
140
|
|
|
*/ |
141
|
24 |
|
private function formatFloat($float, $digits) |
142
|
|
|
{ |
143
|
24 |
|
$digits = max((int) $digits, 1); |
144
|
24 |
|
$string = rtrim(number_format($float, $digits, '.', ''), '0'); |
145
|
|
|
|
146
|
24 |
|
return substr($string, -1) === '.' ? $string . '0' : $string; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|