This commit is contained in:
2020-10-29 11:08:14 +08:00
parent d7c298d1f8
commit 4f16747443
2 changed files with 23 additions and 37 deletions
+9 -24
View File
@@ -6,8 +6,12 @@
* Time: 01:04
*/
declare(strict_types=1);
namespace Snowflake\Core;
use Exception;
/**
* Class JSON
* @package Snowflake\Snowflake\Core
@@ -16,41 +20,22 @@ class JSON
{
/**
* @param $data
* @param string|array|mixed $data
* @return false|string
* @throws \Exception
* @throws Exception
*/
public static function encode($data)
public static function encode(string|array|object $data)
{
if (empty($data)) {
return $data;
}
if (is_array($data)) {
return self::filter(ArrayAccess::toArray($data));
return json_encode(ArrayAccess::toArray($data));
}
return $data;
}
/**
* @param $data
* @return mixed
*/
private static function filter($data)
{
array_walk_recursive($data, function ($value, $key) use ($data) {
if (!is_numeric($value)) {
return;
}
if (is_int(+$value)) {
$value = +$value;
}
$data[$key] = $value;
});
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
/**
* @param $data
* @param bool $asArray
@@ -107,7 +92,7 @@ class JSON
* @param $state
* @param $body
* @return false|int|string
* @throws \Exception
* @throws Exception
*/
public static function output($state, $body)
{