This commit is contained in:
2023-12-12 15:35:38 +08:00
parent 827fb257ab
commit 84c253a9f6
17 changed files with 271 additions and 302 deletions
+73 -75
View File
@@ -10,8 +10,6 @@ declare(strict_types=1);
namespace Kiri\Core;
use Exception;
/**
* Class ArrayAccess
* @package Kiri\Core
@@ -19,82 +17,82 @@ use Exception;
class ArrayAccess
{
/**
* @param $data
* @return array
* @throws Exception
*/
public static function toArray($data): array
{
if (!is_object($data) && !is_array($data)) {
return [];
}
if (is_object($data)) {
$data = self::objToArray($data);
}
$tmp = [];
if (!is_array($data)) {
return $tmp;
}
foreach ($data as $key => $val) {
if (is_array($val) || is_object($val)) {
$tmp[$key] = self::toArray($val);
} else {
$tmp[$key] = $val;
}
}
return $tmp;
}
/**
* @param $data
* @return array
* @throws
*/
public static function toArray($data): array
{
if (!is_object($data) && !is_array($data)) {
return [];
}
if (is_object($data)) {
$data = self::objToArray($data);
}
$tmp = [];
if (!is_array($data)) {
return $tmp;
}
foreach ($data as $key => $val) {
if (is_array($val) || is_object($val)) {
$tmp[$key] = self::toArray($val);
} else {
$tmp[$key] = $val;
}
}
return $tmp;
}
/**
* @param $data
* @return array
* @throws Exception
*/
public static function objToArray($data): array
{
if (!is_object($data)) {
return $data;
}
if (method_exists($data, 'get')) {
$data = $data->get();
if (is_array($data)) {
return $data;
}
}
if (method_exists($data, 'toArray')) {
$data = $data->toArray();
} else {
$data = get_object_vars((object)$data);
}
return $data;
}
/**
* @param $data
* @return array
* @throws
*/
public static function objToArray($data): array
{
if (!is_object($data)) {
return $data;
}
if (method_exists($data, 'get')) {
$data = $data->get();
if (is_array($data)) {
return $data;
}
}
if (method_exists($data, 'toArray')) {
$data = $data->toArray();
} else {
$data = get_object_vars((object)$data);
}
return $data;
}
/**
* @param array $oldArray
* @param array $newArray
* @return array
*/
public static function merge(array $oldArray, array $newArray): array
{
if (empty($oldArray)) {
return $newArray;
} else if (empty($newArray)) {
return $oldArray;
}
foreach ($newArray as $item => $value) {
if (!isset($oldArray[$item])) {
$oldArray[$item] = $value;
}
if (is_array($value)) {
$oldArray[$item] = self::merge($oldArray[$item], $value);
} else {
$oldArray[$item] = $value;
}
}
return $oldArray;
}
/**
* @param array $oldArray
* @param array $newArray
* @return array
*/
public static function merge(array $oldArray, array $newArray): array
{
if (empty($oldArray)) {
return $newArray;
} else if (empty($newArray)) {
return $oldArray;
}
foreach ($newArray as $item => $value) {
if (!isset($oldArray[$item])) {
$oldArray[$item] = $value;
}
if (is_array($value)) {
$oldArray[$item] = self::merge($oldArray[$item], $value);
} else {
$oldArray[$item] = $value;
}
}
return $oldArray;
}
}
+4 -4
View File
@@ -38,7 +38,7 @@ class HashMap implements \ArrayAccess, \IteratorAggregate
* @param string $key
* @param $value
*/
public function put(string $key, $value)
public function put(string $key, $value): void
{
$this->lists[$key] = $value;
}
@@ -78,7 +78,7 @@ class HashMap implements \ArrayAccess, \IteratorAggregate
/**
* @param string $key
*/
public function del(string $key)
public function del(string $key): void
{
if (!$this->has($key)) {
return;
@@ -122,7 +122,7 @@ class HashMap implements \ArrayAccess, \IteratorAggregate
* @param mixed $value
*/
#[ReturnTypeWillChange]
public function offsetSet(mixed $offset, mixed $value)
public function offsetSet(mixed $offset, mixed $value): void
{
$this->put($offset, $value);
}
@@ -132,7 +132,7 @@ class HashMap implements \ArrayAccess, \IteratorAggregate
* @param mixed $offset
*/
#[ReturnTypeWillChange]
public function offsetUnset(mixed $offset)
public function offsetUnset(mixed $offset): void
{
unset($this->lists[$offset]);
}
+2 -2
View File
@@ -61,7 +61,7 @@ class Help
/**
* @param $xml
* @return mixed
* @throws Exception
* @throws
*/
public static function toArray($xml): mixed
{
@@ -111,7 +111,7 @@ class Help
/**
* @param $parameter
* @return array|false|string
* @throws Exception
* @throws
*/
public static function toString($parameter): bool|array|string
{
+121 -121
View File
@@ -20,125 +20,125 @@ use Throwable;
*/
class Json
{
/**
* @param $data
* @return false|string
*/
public static function encode($data): bool|string
{
if (empty($data)) {
return false;
}
if (is_array($data)) {
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
return $data;
}
/**
* @param $data
* @param bool $asArray
* @return mixed
*/
public static function decode($data, bool $asArray = true): mixed
{
if (is_array($data) || is_numeric($data)) {
return $data;
}
if (!is_string($data)) return null;
return json_decode($data, $asArray);
}
/**
* @param string $message
* @param int $code
* @param array|string $data
* @param int $count
* @return string
*/
public static function jsonFail(string $message, int $code = 500, array|string $data = [], int $count = 0): string
{
return json_encode(['code' => $code, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE);
}
/**
* @param string $message
* @param array|string $data
* @param int $count
* @return string
*/
public static function jsonSuccess(array|string $data = [], string $message = "ok", int $count = 0): string
{
return json_encode(['code' => 0, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE);
}
/**
* @param $code
* @param string|array $message
* @param array|int $data
* @param int $count
* @param array $exPageInfo
* @return string|bool
*/
public static function to($code, string|array $message = '', array|int $data = [], int $count = 0, array $exPageInfo = []): string|bool
{
$params['code'] = $code;
if (!is_string($message)) {
$params['message'] = 'System success.';
$params['param'] = $message;
$params['exPageInfo'] = $data;
} else {
$params['message'] = $message;
$params['param'] = $data;
}
if (!empty($exPageInfo)) {
$params['exPageInfo'] = $exPageInfo;
}
$params['count'] = $count;
if (is_numeric($data) || !is_numeric($count)) {
$params['count'] = $data;
$params['exPageInfo'] = $count;
}
if ((int)$params['count'] == -100) {
$params['count'] = 1;
}
return static::encode($params);
}
/**
* @param Throwable|Error $throwable
* @return bool|string
*/
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
* @return false|int|string
* @throws Exception
*/
public static function output($state, $body): bool|int|string
{
$params['state'] = $state;
$params['body'] = ArrayAccess::toArray($body);
return static::encode($params);
}
/**
* @param $data
* @return false|string
*/
public static function encode($data): bool|string
{
if (empty($data)) {
return false;
}
if (is_array($data)) {
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
return $data;
}
/**
* @param $data
* @param bool $asArray
* @return mixed
*/
public static function decode($data, bool $asArray = true): mixed
{
if (is_array($data) || is_numeric($data)) {
return $data;
}
if (!is_string($data)) return null;
return json_decode($data, $asArray);
}
/**
* @param string $message
* @param int $code
* @param array|string $data
* @param int $count
* @return string
*/
public static function jsonFail(string $message, int $code = 500, array|string $data = [], int $count = 0): string
{
return json_encode(['code' => $code, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE);
}
/**
* @param string $message
* @param array|string $data
* @param int $count
* @return string
*/
public static function jsonSuccess(array|string $data = [], string $message = "ok", int $count = 0): string
{
return json_encode(['code' => 0, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE);
}
/**
* @param $code
* @param string|array $message
* @param array|int $data
* @param int $count
* @param array $exPageInfo
* @return string|bool
*/
public static function to($code, string|array $message = '', array|int $data = [], int $count = 0, array $exPageInfo = []): string|bool
{
$params['code'] = $code;
if (!is_string($message)) {
$params['message'] = 'System success.';
$params['param'] = $message;
$params['exPageInfo'] = $data;
} else {
$params['message'] = $message;
$params['param'] = $data;
}
if (!empty($exPageInfo)) {
$params['exPageInfo'] = $exPageInfo;
}
$params['count'] = $count;
if (is_numeric($data) || !is_numeric($count)) {
$params['count'] = $data;
$params['exPageInfo'] = $count;
}
if ((int)$params['count'] == -100) {
$params['count'] = 1;
}
return static::encode($params);
}
/**
* @param Throwable|Error $throwable
* @return bool|string
*/
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
* @return false|int|string
* @throws
*/
public static function output($state, $body): bool|int|string
{
$params['state'] = $state;
$params['body'] = ArrayAccess::toArray($body);
return static::encode($params);
}
}
+6 -6
View File
@@ -14,9 +14,9 @@ use Exception;
class Str
{
const STRING = 'abcdefghijklmnopqrstuvwxyz';
const string STRING = 'abcdefghijklmnopqrstuvwxyz';
const NUMBER = '01234567890';
const string NUMBER = '01234567890';
/**
* @param int $length
@@ -107,7 +107,7 @@ class Str
*/
public static function isSerialize($data, $callBack = NULL): bool
{
$false = !empty($data) && swoole_unserialize($data) !== FALSE;
$false = !empty($data) && unserialize($data) !== FALSE;
if ($false && is_callable($callBack, TRUE)) {
return call_user_func($callBack, $data);
}
@@ -172,10 +172,10 @@ class Str
public static function filename($file, $type): string
{
return match ($type) {
'image/png' => md5_file($file) . '.png',
'image/png' => md5_file($file) . '.png',
'image/jpeg', 'image/jpg' => md5_file($file) . '.jpg',
'image/gif' => md5_file($file) . '.gif',
default => md5_file($file),
'image/gif' => md5_file($file) . '.gif',
default => md5_file($file),
};
}
+34 -34
View File
@@ -18,40 +18,40 @@ use Exception;
class Xml
{
/**
* @param $data
* @param bool $asArray
* @return array|object
* @throws Exception
*/
public static function toArray($data, bool $asArray = true): object|array
{
$data = \simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($data === false) {
throw new Exception('Parameter format error.');
}
$array = \get_object_vars($data);
if (isset($array[0])) {
$array[$data->getName()] = $array[0];
unset($array[0]);
}
return $array;
}
/**
* @param $data
* @param bool $asArray
* @return array|object
* @throws
*/
public static function toArray($data, bool $asArray = true): object|array
{
$data = \simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($data === false) {
throw new Exception('Parameter format error.');
}
$array = \get_object_vars($data);
if (isset($array[0])) {
$array[$data->getName()] = $array[0];
unset($array[0]);
}
return $array;
}
/**
* @param $str
* @return array|bool|object
* @throws Exception
*/
public static function isXml($str): object|bool|array
{
$xml_parser = \xml_parser_create();
if (!\xml_parse($xml_parser, $str, true)) {
\xml_parser_free($xml_parser);
return false;
} else {
return self::toArray($str);
}
}
/**
* @param $str
* @return array|bool|object
* @throws
*/
public static function isXml($str): object|bool|array
{
$xml_parser = \xml_parser_create();
if (!\xml_parse($xml_parser, $str, true)) {
\xml_parser_free($xml_parser);
return false;
} else {
return self::toArray($str);
}
}
}