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-2020 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 | public function getDefaultOptions() |
||
24 | { |
||
25 | return self::$defaultOptions; |
||
26 | } |
||
27 | |||
28 | public function supports($value) |
||
29 | { |
||
30 | return \is_float($value); |
||
31 | } |
||
32 | |||
33 | public function encode($value, $depth, array $options, callable $encode) |
||
34 | { |
||
35 | if (is_nan($value)) { |
||
36 | return 'NAN'; |
||
37 | } elseif (is_infinite($value)) { |
||
38 | return $value < 0 ? '-INF' : 'INF'; |
||
39 | } |
||
40 | |||
41 | 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 | private function encodeNumber($float, array $options, callable $encode) |
||
52 | { |
||
53 | if ($this->isInteger($float, $options['float.integers'])) { |
||
54 | return $this->encodeInteger($float, $encode); |
||
55 | } elseif ($float === 0.0) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
56 | return '0.0'; |
||
57 | } elseif ($options['float.export']) { |
||
58 | return var_export((float) $float, true); |
||
59 | } |
||
60 | |||
61 | 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 | private function isInteger($float, $allowIntegers) |
||
71 | { |
||
72 | if (!$allowIntegers || round($float) !== $float) { |
||
73 | return false; |
||
74 | } elseif (abs($float) < self::FLOAT_MAX) { |
||
75 | return true; |
||
76 | } |
||
77 | |||
78 | 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 | private function encodeInteger($float, callable $encode) |
||
88 | { |
||
89 | $minimum = \defined('PHP_INT_MIN') ? \PHP_INT_MIN : ~\PHP_INT_MAX; |
||
90 | |||
91 | if ($float >= $minimum && $float <= \PHP_INT_MAX) { |
||
92 | return $encode((int) $float); |
||
93 | } |
||
94 | |||
95 | 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 | private function determinePrecision($options) |
||
104 | { |
||
105 | $precision = $options['float.precision']; |
||
106 | |||
107 | if ($precision === false) { |
||
108 | $precision = ini_get('serialize_precision'); |
||
109 | } |
||
110 | |||
111 | 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 | private function encodeFloat($float, $precision) |
||
121 | { |
||
122 | $log = (int) floor(log(abs($float), 10)); |
||
123 | |||
124 | if ($log > -5 && abs($float) < self::FLOAT_MAX && abs($log) < $precision) { |
||
125 | return $this->formatFloat($float, $precision - $log - 1); |
||
126 | } |
||
127 | |||
128 | // Deal with overflow that results from rounding |
||
129 | $log += (int) (round(abs($float) / 10 ** $log, $precision - 1) / 10); |
||
130 | $string = $this->formatFloat($float / 10 ** $log, $precision - 1); |
||
131 | |||
132 | 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 | private function formatFloat($float, $digits) |
||
142 | { |
||
143 | $digits = max((int) $digits, 1); |
||
144 | $string = rtrim(number_format($float, $digits, '.', ''), '0'); |
||
145 | |||
146 | return substr($string, -1) === '.' ? $string . '0' : $string; |
||
147 | } |
||
148 | } |
||
149 |