This commit is contained in:
2021-02-08 16:52:27 +08:00
parent da35f038c5
commit 0e31371651
3 changed files with 64 additions and 31 deletions
+26 -3
View File
@@ -6,6 +6,7 @@ namespace Snowflake\Core;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
@@ -19,11 +20,13 @@ class Help
* @param array $data
* @return string
*/
public static function toXml(array $data)
#[Pure] public static function toXml(array $data): string
{
$xml = "<xml>";
foreach ($data as $key => $val) {
if (is_numeric($val)) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . static::xmlChild($val) . "</" . $key . ">";
} else if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
@@ -34,6 +37,26 @@ class Help
}
/**
* @param array $array
* @return string
*/
private static function xmlChild(array $array): string
{
$string = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$string .= static::xmlChild($value);
} else if (is_numeric($value)) {
$string .= "<" . $key . ">" . $value . "</" . $key . ">";
} else {
$string .= "<" . $key . "><![CDATA[" . $value . "]]></" . $key . ">";
}
}
return $string;
}
/**
* @param $xml
* @return mixed
@@ -151,7 +174,7 @@ class Help
* @param $type
* @return string
*/
public static function sign(array $array, $key, $type): string
public static function sign(array $array, $key, $type = 'MD5'): string
{
ksort($array, SORT_ASC);
$string = [];
+22 -2
View File
@@ -10,7 +10,9 @@ declare(strict_types=1);
namespace Snowflake\Core;
use Error;
use Exception;
use Throwable;
/**
* Class JSON
@@ -19,10 +21,10 @@ use Exception;
class Json
{
/**
* @param $data
* @return false|string
* @throws Exception
*/
public static function encode($data): bool|string
{
@@ -49,6 +51,7 @@ class Json
return json_decode($data, $asArray);
}
/**
* @param $code
* @param string $message
@@ -85,6 +88,24 @@ class Json
return static::encode($params);
}
/**
* @param Throwable|Error $throwable
* @return bool|string
* @throws Exception
*/
public static function error(Throwable|Error $throwable): bool|string
{
$array['code'] = $throwable->getCode() == 0 ? 500 : $throwable->getCode();
$array['message'] = $throwable->getMessage();
$array['param'] = [
'file' => $throwable->getFile(),
'line' => $throwable->getLine()
];
return Json::encode($array);
}
/**
* @param $state
* @param $body
@@ -98,5 +119,4 @@ class Json
return static::encode($params);
}
}