.gitattributes000064400000000054151360551000007432 0ustar00/tests export-ignore /.github export-ignore LICENSE000064400000002054151360551000005546 0ustar00The MIT License (MIT) Copyright (c) Hyperf Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. composer.json000064400000002023151360551000007257 0ustar00{ "name": "hyperf/codec", "description": "A codec component for Hyperf.", "license": "MIT", "keywords": [ "php", "swoole", "hyperf", "codec" ], "homepage": "https://hyperf.io", "support": { "docs": "https://hyperf.wiki", "issues": "https://github.com/hyperf/hyperf/issues", "pull-request": "https://github.com/hyperf/hyperf/pulls", "source": "https://github.com/hyperf/hyperf" }, "require": { "ext-json": "*", "ext-xml": "*", "php": ">=8.0", "hyperf/contract": "~3.0.0" }, "suggest": { "ext-igbinary": "Required to use IgbinarySerializerPacker." }, "autoload": { "psr-4": { "Hyperf\\Codec\\": "src/" } }, "autoload-dev": { "psr-4": { "HyperfTest\\Codec\\": "tests/" } }, "config": { "sort-packages": true }, "extra": { "branch-alias": { "dev-master": "3.0-dev" } } } src/Base62.php000064400000002004151360551000007056 0ustar00 0) { $remain = $number % self::BASE; $chars[] = self::CHARS[$remain]; $number = ($number - $remain) / self::BASE; } return implode('', array_reverse($chars)); } public static function decode(string $data): int { return array_reduce(array_map(function ($character) { return strpos(self::CHARS, $character); }, str_split($data)), function ($result, $remain) { return $result * self::BASE + $remain; }); } } src/Exception/InvalidArgumentException.php000064400000000526151360551000014751 0ustar00toArray(); } try { $json = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth); } catch (Throwable $exception) { throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception); } return $json; } /** * @throws InvalidArgumentException */ public static function decode(string $json, bool $assoc = true, int $depth = 512, int $flags = 0): mixed { try { $decode = json_decode($json, $assoc, $depth, $flags | JSON_THROW_ON_ERROR); } catch (Throwable $exception) { throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception); } return $decode; } } src/Packer/IgbinarySerializerPacker.php000064400000001062151360551000014170 0ustar00toArray(); } else { $data = (array) $data; } if ($parentNode === null) { $xml = new SimpleXMLElement('' . "<{$root}>"); } else { $xml = $parentNode; } foreach ($data as $key => $value) { if (is_array($value)) { self::toXml($value, $xml->addChild($key)); } else { if (is_numeric($key)) { $xml->addChild('item' . $key, (string) $value); } else { $xml->addChild($key, (string) $value); } } } return trim($xml->asXML()); } public static function toArray($xml): array { $respObject = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR); if ($respObject === false) { throw new InvalidArgumentException('Syntax error.'); } return json_decode(json_encode($respObject), true); } }