代码初始化
This commit is contained in:
39
addons/epay/library/hyperf/utils/src/Codec/Base62.php
Normal file
39
addons/epay/library/hyperf/utils/src/Codec/Base62.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Utils\Codec;
|
||||
|
||||
class Base62
|
||||
{
|
||||
public const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
|
||||
public const BASE = 62;
|
||||
|
||||
public static function encode(int $number): string
|
||||
{
|
||||
$chars = [];
|
||||
while ($number > 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
56
addons/epay/library/hyperf/utils/src/Codec/Json.php
Normal file
56
addons/epay/library/hyperf/utils/src/Codec/Json.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Utils\Codec;
|
||||
|
||||
use Hyperf\Utils\Contracts\Arrayable;
|
||||
use Hyperf\Utils\Contracts\Jsonable;
|
||||
use Hyperf\Utils\Exception\InvalidArgumentException;
|
||||
|
||||
class Json
|
||||
{
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function encode($data, int $flags = JSON_UNESCAPED_UNICODE, int $depth = 512): string
|
||||
{
|
||||
if ($data instanceof Jsonable) {
|
||||
return (string) $data;
|
||||
}
|
||||
|
||||
if ($data instanceof Arrayable) {
|
||||
$data = $data->toArray();
|
||||
}
|
||||
|
||||
try {
|
||||
$json = json_encode($data, $flags | JSON_THROW_ON_ERROR, $depth);
|
||||
} catch (\Throwable $exception) {
|
||||
throw new InvalidArgumentException($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function decode(string $json, bool $assoc = true, int $depth = 512, int $flags = 0)
|
||||
{
|
||||
try {
|
||||
$decode = json_decode($json, $assoc, $depth, $flags | JSON_THROW_ON_ERROR);
|
||||
} catch (\Throwable $exception) {
|
||||
throw new InvalidArgumentException($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
|
||||
return $decode;
|
||||
}
|
||||
}
|
||||
70
addons/epay/library/hyperf/utils/src/Codec/Xml.php
Normal file
70
addons/epay/library/hyperf/utils/src/Codec/Xml.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Utils\Codec;
|
||||
|
||||
use Hyperf\Utils\Contracts\Arrayable;
|
||||
use Hyperf\Utils\Contracts\Xmlable;
|
||||
use Hyperf\Utils\Exception\InvalidArgumentException;
|
||||
use SimpleXMLElement;
|
||||
|
||||
class Xml
|
||||
{
|
||||
public static function toXml($data, $parentNode = null, $root = 'root')
|
||||
{
|
||||
if ($data instanceof Xmlable) {
|
||||
return (string) $data;
|
||||
}
|
||||
if ($data instanceof Arrayable) {
|
||||
$data = $data->toArray();
|
||||
} else {
|
||||
$data = (array) $data;
|
||||
}
|
||||
if ($parentNode === null) {
|
||||
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?>' . "<{$root}></{$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)
|
||||
{
|
||||
// For PHP 8.0, libxml_disable_entity_loader() has been deprecated.
|
||||
// As libxml 2.9.0 is now required, external entity loading is guaranteed to be disabled by default.
|
||||
// And this function is no longer needed to protect against XXE attacks, unless the (still vulnerable). LIBXML_NOENT is used.
|
||||
// In that case, it is recommended to refactor the code using libxml_set_external_entity_loader() to suppress loading of external entities.
|
||||
if (\PHP_VERSION_ID < 80000) {
|
||||
$disableLibxmlEntityLoader = libxml_disable_entity_loader(true);
|
||||
$respObject = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);
|
||||
libxml_disable_entity_loader($disableLibxmlEntityLoader);
|
||||
} else {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user