diff options
author | Mario <mario@mariovavti.com> | 2024-01-03 11:09:22 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2024-01-03 11:09:22 +0000 |
commit | 16e6eec3fb2bfb5a34c9dc94d46232be8461a947 (patch) | |
tree | d2fcf420e31f65e0c1a7e74529b65c6df6ae450e /vendor/mmccook/php-json-canonicalization-scheme/src/Utils.php | |
parent | afe8552be66a05b379ddc7b5f78e40f8a76540b8 (diff) | |
download | volse-hubzilla-16e6eec3fb2bfb5a34c9dc94d46232be8461a947.tar.gz volse-hubzilla-16e6eec3fb2bfb5a34c9dc94d46232be8461a947.tar.bz2 volse-hubzilla-16e6eec3fb2bfb5a34c9dc94d46232be8461a947.zip |
composer add mmccook/php-json-canonicalization-scheme
Diffstat (limited to 'vendor/mmccook/php-json-canonicalization-scheme/src/Utils.php')
-rw-r--r-- | vendor/mmccook/php-json-canonicalization-scheme/src/Utils.php | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/mmccook/php-json-canonicalization-scheme/src/Utils.php b/vendor/mmccook/php-json-canonicalization-scheme/src/Utils.php new file mode 100644 index 000000000..57079df3b --- /dev/null +++ b/vendor/mmccook/php-json-canonicalization-scheme/src/Utils.php @@ -0,0 +1,52 @@ +<?php + +namespace Mmccook\JsonCanonicalizator; + +class Utils +{ + /** + * @param array $array + * @return bool + */ + public static function isAssoc(array $array): bool + { + $keys = array_keys($array); + + return array_keys($keys) !== $keys; + } + + public static function asHex(string $data): string + { + return rtrim(chunk_split(bin2hex($data), 2, ' ')); + } + + public static function es6NumberFormat(float $number): string + { + + if (is_nan($number) || is_infinite($number)) { + throw new \RuntimeException("can't use Nan or Infinity in json"); + } + + if (0.0 === $number) { + return '0'; + } + + $sign = ''; + if ($number < 0) { + $sign = '-'; + $number = -$number; + } + + if ($number < 1e+21 && $number >= 1e-6) { + $formatted = number_format($number, 7, '.', ''); + $formatted = rtrim($formatted, '.0'); + } else { + $formatted = sprintf('%e', $number); + $parts = explode('e', $formatted); + $parts[0] = rtrim($parts[0], '.0'); + $formatted = implode('e', $parts); + } + + return $sign . $formatted; + } +} |