hyperf-gateway/app/Helper/Result.php
2022-11-04 15:37:04 +08:00

47 lines
977 B
PHP

<?php
declare(strict_types=1);
namespace App\Helper;
use App\Constants\ErrorCode;
/**
* 返回处理公共类
*/
class Result
{
/**
* 返回数据格式化
*
* @param array|null $data
* @param int $code
* @param string $message
* @return array
*/
public static function result(?array $data = null, int $code = 200, string $message = 'success') : array
{
return [
'code' => $code,
'data' => $data ?? [],
'msg' => $message
];
}
/**
* 错误返回
*
* @param int $code
* @param string|null $message
* @param array|null $data
* @return array
*/
public static function error(int $code = ErrorCode::SERVER_ERROR, ?string $message = null, ?array $data = null) : array
{
if ($message === null) {
$message = ErrorCode::getMessage($code);
}
return self::result($data, $code, $message);
}
}