diff options
author | Mario <mario@mariovavti.com> | 2024-03-14 10:13:22 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2024-03-14 10:13:22 +0000 |
commit | 55097c47c5534d4453f7494f8a1542f7beb4d588 (patch) | |
tree | 399f81bbd03fcb4bb713339d06512eee962ec8f6 /vendor/mmccook/php-json-canonicalization-scheme/src/JsonCanonicalizator.php | |
parent | 97b82fc77b424d051b2a472ab2318fd768151bdd (diff) | |
download | volse-hubzilla-55097c47c5534d4453f7494f8a1542f7beb4d588.tar.gz volse-hubzilla-55097c47c5534d4453f7494f8a1542f7beb4d588.tar.bz2 volse-hubzilla-55097c47c5534d4453f7494f8a1542f7beb4d588.zip |
Revert "composer update and use the fixed streams php-jcs library until the floats issue will be fixed upstream. see here for reference https://codeberg.org/streams/streams/issues/151"
This reverts commit 6bf61dfa6b585db01b607a79bd64ec9c583a9c10.
Diffstat (limited to 'vendor/mmccook/php-json-canonicalization-scheme/src/JsonCanonicalizator.php')
-rw-r--r-- | vendor/mmccook/php-json-canonicalization-scheme/src/JsonCanonicalizator.php | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/vendor/mmccook/php-json-canonicalization-scheme/src/JsonCanonicalizator.php b/vendor/mmccook/php-json-canonicalization-scheme/src/JsonCanonicalizator.php new file mode 100644 index 000000000..6a3c82959 --- /dev/null +++ b/vendor/mmccook/php-json-canonicalization-scheme/src/JsonCanonicalizator.php @@ -0,0 +1,79 @@ +<?php + +namespace Mmccook\JsonCanonicalizator; + +class JsonCanonicalizator implements JsonCanonicalizatorInterface +{ + public const JSON_FLAGS = \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES; + + /** + * @param $data + * @param bool $asHex + * @return string + */ + public function canonicalize($data, bool $asHex = false): string + { + ob_start(); + + $this->serialize($data); + + $result = ob_get_clean(); + + return $asHex ? Utils::asHex($result) : $result; + } + + private function serialize($item) + { + if (is_float($item)) { + echo Utils::es6NumberFormat($item); + + return; + } + + if (null === $item || is_scalar($item)) { + echo json_encode($item, self::JSON_FLAGS); + + return; + } + + if (is_array($item) && ! Utils::isAssoc($item)) { + echo '['; + $next = false; + foreach ($item as $element) { + if ($next) { + echo ','; + } + $next = true; + $this->serialize($element); + } + echo ']'; + + return; + } + + if (is_object($item)) { + $item = (array)$item; + } + + uksort($item, function (string $a, string $b) { + $a = mb_convert_encoding($a, 'UTF-16BE'); + $b = mb_convert_encoding($b, 'UTF-16BE'); + + return strcmp($a, $b); + }); + + echo '{'; + $next = false; + foreach ($item as $key => $value) { + //var_dump($key, $value); + if ($next) { + echo ','; + } + $next = true; + $outKey = json_encode((string)$key, self::JSON_FLAGS); + echo $outKey, ':', $this->serialize($value); + } + echo '}'; + + } +} |