add clear

This commit is contained in:
2022-09-09 16:42:55 +08:00
parent ad76b45b45
commit 7273fe1ce5
63 changed files with 2625 additions and 5287 deletions
+295 -271
View File
@@ -12,7 +12,7 @@ class Config
*
* 小程序ID
*/
private $appid = '';
private string $appid = '';
/**
@@ -20,357 +20,470 @@ class Config
*
* 商户号ID
*/
private $mch_id = '';
private string $mch_id = '';
/**
* @var string
*
* 设备号
*/
private $device_info = 'WEB';
private string $device_info = 'WEB';
private string $token;
private string $encodingAesKey;
private string $schema = 'WECHATPAY2-SHA256-RSA2048';
private string $serial_no = '33F00000E4248E2EB3C61FC8384E1E155ECBB1B2';
private $token;
private $encodingAesKey;
/**
* @var string
*
* 随机字符串
*/
private $nonce_str = '';
private string $nonce_str = '';
/**
* @var string
*
* 商品简单描述
*/
private $body = '好友默契Pk充值!';
private string $body = '好友默契Pk充值!';
/**
* @var string
*
* 商户订单号
*/
private $out_trade_no = "";
private string $out_trade_no = "";
/**
* @var int
* @var int|float
*
* 金额
*/
private $total_fee = 0;
private float|int $total_fee = 0;
/**
* @var string
*
* 终端IP
*/
private $spbill_create_ip = "";
private string $spbill_create_ip = "";
/**
* @var string
*
* 异步回调地址
*/
private $notify_url = "";
private string $notify_url = "";
/**
* @var string
*
* 交易类型
*/
private $trade_type = 'JSAPI';
private string $trade_type = 'JSAPI';
/**
* @var string
*
* 签名方式
*/
private $sign_type = 'MD5';
private string $sign_type = 'MD5';
/**
* @var string
*
* 商户接口地址
*/
private $mch_host = 'https://api.mch.weixin.qq.com';
private string $mch_host = 'https://api.mch.weixin.qq.com';
/**
* @var string
*/
private $appsecret = '';
private string $appsecret = '';
private $remote_addr = '127.0.0.1';
private string $remote_addr = '127.0.0.1';
private $ssl_cert = '';
private $ssl_key = '';
private $ssl_ca = '';
private $port = '';
private string $ssl_cert = '';
private string $ssl_key = '';
private string $ssl_ca = '';
private int $port = 443;
/**
* @var string
*/
private $key = '';
private $access_token = '';
private $agent = '';
private $usrSwoole = false;
private string $key = '';
private string $access_token = '';
private string $agent = '';
private bool $usrSwoole = false;
/**
* @return string
*/
public function getSslCa()
public function getSchema(): string
{
return $this->ssl_ca;
return $this->schema;
}
/**
* @param $ssl_ca
* @return Config
* @param string $schema
*/
public function setSslCa($ssl_ca): Config
public function setSchema(string $schema): void
{
$this->ssl_ca = $ssl_ca;
return $this;
$this->schema = $schema;
}
/**
* @return string
*/
public function getPort()
public function getSerialNo(): string
{
return $this->port;
return $this->serial_no;
}
/**
* @param $port
* @return Config
* @param string $serial_no
*/
public function setPort($port): Config
public function setSerialNo(string $serial_no): void
{
$this->port = $port;
return $this;
}
/**
* @return bool
*/
public function isUsrSwoole()
{
return $this->usrSwoole;
}
/**
* @param bool $usrSwoole
* @return Config
*/
public function setUsrSwoole(bool $usrSwoole): Config
{
$this->usrSwoole = $usrSwoole;
return $this;
$this->serial_no = $serial_no;
}
/**
* @return string
*/
public function getAgent()
public function getAppid(): string
{
return $this->agent;
return $this->appid;
}
/**
* @param $agent
* @return Config
* @param string $appid
*/
public function setAgent($agent): Config
{
$this->agent = $agent;
return $this;
}
/**
* @return mixed
*/
public function getToken()
{
return $this->token;
}
/**
* @param mixed $token
* @return Config
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* @return mixed
*/
public function getEncodingAesKey()
{
return $this->encodingAesKey;
}
/**
* @param mixed $encodingAesKey
* @return Config
*/
public function setEncodingAesKey($encodingAesKey)
{
$this->encodingAesKey = $encodingAesKey;
return $this;
}
/**
* @return string
*/
public function getAccessToken()
{
return $this->access_token;
}
/**
* @param $access_token
*/
public function setAccessToken($access_token)
{
$this->access_token = $access_token;
}
/**
* @param $remote_addr
*/
public function setRemoteAddr($remote_addr)
{
$this->remote_addr = $remote_addr;
}
/**
* @param $appid
*/
public function setAppid($appid)
public function setAppid(string $appid): void
{
$this->appid = $appid;
}
/**
* @param $mch_id
* @return string
*/
public function setMchId($mch_id)
public function getMchId(): string
{
return $this->mch_id;
}
/**
* @param string $mch_id
*/
public function setMchId(string $mch_id): void
{
$this->mch_id = $mch_id;
}
/**
* @param $device_info
* @return string
*/
public function setDeviceInfo($device_info)
public function getDeviceInfo(): string
{
return $this->device_info;
}
/**
* @param string $device_info
*/
public function setDeviceInfo(string $device_info): void
{
$this->device_info = $device_info;
}
/**
* @param $nonce_str
* @return string
*/
public function setNonceStr($nonce_str)
public function getToken(): string
{
return $this->token;
}
/**
* @param string $token
*/
public function setToken(string $token): void
{
$this->token = $token;
}
/**
* @return string
*/
public function getEncodingAesKey(): string
{
return $this->encodingAesKey;
}
/**
* @param string $encodingAesKey
*/
public function setEncodingAesKey(string $encodingAesKey): void
{
$this->encodingAesKey = $encodingAesKey;
}
/**
* @return string
*/
public function getNonceStr(): string
{
return $this->nonce_str;
}
/**
* @param string $nonce_str
*/
public function setNonceStr(string $nonce_str): void
{
$this->nonce_str = $nonce_str;
}
/**
* @param $body
* @return string
*/
public function setBody($body)
public function getBody(): string
{
return $this->body;
}
/**
* @param string $body
*/
public function setBody(string $body): void
{
$this->body = $body;
}
/**
* @param $out_trade_no
* @return string
*/
public function setOutTradeNo($out_trade_no)
public function getOutTradeNo(): string
{
return $this->out_trade_no;
}
/**
* @param string $out_trade_no
*/
public function setOutTradeNo(string $out_trade_no): void
{
$this->out_trade_no = $out_trade_no;
}
/**
* @return int
*/
public function getTotalFee(): int|string
{
return $this->total_fee;
}
/**
* @param int $total_fee
*/
public function setTotalFee(int $total_fee)
public function setTotalFee(int|string $total_fee): void
{
$this->total_fee = $total_fee;
}
/**
* @param $spbill_create_ip
* @return string
*/
public function setSpbillCreateIp($spbill_create_ip)
public function getSpbillCreateIp(): string
{
return $this->spbill_create_ip;
}
/**
* @param string $spbill_create_ip
*/
public function setSpbillCreateIp(string $spbill_create_ip): void
{
$this->spbill_create_ip = $spbill_create_ip;
}
/**
* @param $notify_url
* @return string
*/
public function setNotifyUrl($notify_url)
public function getNotifyUrl(): string
{
return $this->notify_url;
}
/**
* @param string $notify_url
*/
public function setNotifyUrl(string $notify_url): void
{
$this->notify_url = $notify_url;
}
/**
* @param $trade_type
* @return string
*/
public function setTradeType($trade_type)
public function getTradeType(): string
{
return $this->trade_type;
}
/**
* @param string $trade_type
*/
public function setTradeType(string $trade_type): void
{
$this->trade_type = $trade_type;
}
/**
* @param $sign_type
* @return string
*/
public function setSignType($sign_type)
public function getSignType(): string
{
return $this->sign_type;
}
/**
* @param string $sign_type
*/
public function setSignType(string $sign_type): void
{
$this->sign_type = $sign_type;
}
/**
* @param $mch_host
* @return string
*/
public function setMchHost($mch_host)
public function getMchHost(): string
{
return $this->mch_host;
}
/**
* @param string $mch_host
*/
public function setMchHost(string $mch_host): void
{
$this->mch_host = $mch_host;
}
/**
* @param $appsecret
* @return string
*/
public function setAppsecret($appsecret)
public function getAppsecret(): string
{
return $this->appsecret;
}
/**
* @param string $appsecret
*/
public function setAppsecret(string $appsecret): void
{
$this->appsecret = $appsecret;
}
/**
* @param $ssl_cert
* @return string
*/
public function setSslCert($ssl_cert)
public function getRemoteAddr(): string
{
return $this->remote_addr;
}
/**
* @param string $remote_addr
*/
public function setRemoteAddr(string $remote_addr): void
{
$this->remote_addr = $remote_addr;
}
/**
* @return string
*/
public function getSslCert(): string
{
return $this->ssl_cert;
}
/**
* @param string $ssl_cert
*/
public function setSslCert(string $ssl_cert): void
{
$this->ssl_cert = $ssl_cert;
}
/**
* @param $ssl_key
* @return string
*/
public function setSslKey($ssl_key)
public function getSslKey(): string
{
return $this->ssl_key;
}
/**
* @param string $ssl_key
*/
public function setSslKey(string $ssl_key): void
{
$this->ssl_key = $ssl_key;
}
/**
* @param $key
* @return string
*/
public function setKey($key)
public function getSslCa(): string
{
return $this->ssl_ca;
}
/**
* @param string $ssl_ca
*/
public function setSslCa(string $ssl_ca): void
{
$this->ssl_ca = $ssl_ca;
}
/**
* @return string
*/
public function getPort(): string
{
return $this->port;
}
/**
* @param string $port
*/
public function setPort(string $port): void
{
$this->port = $port;
}
/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}
/**
* @param string $key
*/
public function setKey(string $key): void
{
$this->key = $key;
}
@@ -378,145 +491,56 @@ class Config
/**
* @return string
*/
public function getAppid()
public function getAccessToken(): string
{
return $this->appid;
return $this->access_token;
}
/**
* @param string $access_token
*/
public function setAccessToken(string $access_token): void
{
$this->access_token = $access_token;
}
/**
* @return string
*/
public function getMchId()
public function getAgent(): string
{
return $this->mch_id;
return $this->agent;
}
/**
* @return string
* @param string $agent
*/
public function getDeviceInfo()
public function setAgent(string $agent): void
{
return $this->device_info;
$this->agent = $agent;
}
/**
* @return string
* @return bool
*/
public function getNonceStr()
public function isUsrSwoole(): bool
{
return $this->nonce_str;
return $this->usrSwoole;
}
/**
* @return string
* @param bool $usrSwoole
*/
public function getBody()
public function setUsrSwoole(bool $usrSwoole): void
{
return $this->body;
$this->usrSwoole = $usrSwoole;
}
/**
* @return string
*/
public function getOutTradeNo()
{
return $this->out_trade_no;
}
/**
* @return int
*/
public function getTotalFee()
{
return $this->total_fee;
}
/**
* @return string
*/
public function getSpbillCreateIp()
{
return $this->spbill_create_ip;
}
/**
* @return string
*/
public function getNotifyUrl()
{
return $this->notify_url;
}
/**
* @return string
*/
public function getTradeType()
{
return $this->trade_type;
}
/**
* @return string
*/
public function getSignType()
{
return $this->sign_type;
}
/**
* @return string
*/
public function getMchHost()
{
return $this->mch_host;
}
/**
* @return string
*/
public function getAppsecret()
{
return $this->appsecret;
}
/**
* @return string
*/
public function getRemoteAddr()
{
return $this->remote_addr;
}
/**
* @return string
*/
public function getSslCert()
{
return $this->ssl_cert;
}
/**
* @return string
*/
public function getSslKey()
{
return $this->ssl_key;
}
/**
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* @param array $configs
* @return $this
*/
public function setConfigs(array $configs)
public function setConfigs(array $configs): static
{
if (empty($configs)) {
return $this;
+51 -33
View File
@@ -7,56 +7,80 @@ namespace wchat\common;
class Decode
{
private $sessionKey;
private $iv;
private $encryptedData;
private $appId;
private string $sessionKey;
private string $iv;
private string $encryptedData;
private string $appId;
private $OK = 0;
private $IllegalAesKey = -41001;
private $IllegalIv = -41002;
private $IllegalBuffer = -41003;
private $DecodeBase64Error = -41004;
private int $OK = 0;
private int $IllegalAesKey = -41001;
private int $IllegalIv = -41002;
private int $IllegalBuffer = -41003;
private int $DecodeBase64Error = -41004;
/**
* @param mixed $sessionKey
* @return Decode
* @return string
*/
public function setSessionKey($sessionKey)
public function getSessionKey(): string
{
return $this->sessionKey;
}
/**
* @param string $sessionKey
*/
public function setSessionKey(string $sessionKey): void
{
$this->sessionKey = $sessionKey;
return $this;
}
/**
* @param mixed $iv
* @return Decode
* @return string
*/
public function setIv($iv)
public function getIv(): string
{
return $this->iv;
}
/**
* @param string $iv
*/
public function setIv(string $iv): void
{
$this->iv = $iv;
return $this;
}
/**
* @param mixed $encryptedData
* @return Decode
* @return string
*/
public function setEncryptedData($encryptedData)
public function getEncryptedData(): string
{
return $this->encryptedData;
}
/**
* @param string $encryptedData
*/
public function setEncryptedData(string $encryptedData): void
{
$this->encryptedData = $encryptedData;
return $this;
}
/**
* @param mixed $appId
* @return Decode
* @return string
*/
public function setAppId($appId)
public function getAppId(): string
{
return $this->appId;
}
/**
* @param string $appId
*/
public function setAppId(string $appId): void
{
$this->appId = $appId;
return $this;
}
@@ -65,7 +89,7 @@ class Decode
* @return array|mixed
* @throws \Exception
*/
public function decode($asArray)
public function decode($asArray): object|array
{
if (strlen($this->sessionKey) != 24) {
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
@@ -87,13 +111,7 @@ class Decode
if ($dataObj->watermark->appid != $this->appId) {
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
}
if ($asArray) {
return get_object_vars($dataObj);
}
return $dataObj;
return $asArray ? get_object_vars($dataObj) : $dataObj;
}
}
+25 -47
View File
@@ -4,14 +4,14 @@
namespace wchat\common;
class Help extends Miniprogarampage
class Help extends Multiprogramming
{
/**
* @param array $data
* @return string
*/
public static function toXml(array $data)
public static function toXml(array $data): string
{
$xml = "<xml>";
foreach ($data as $key => $val) {
@@ -34,7 +34,7 @@ class Help extends Miniprogarampage
* @param array $array
* @return string
*/
private static function xmlChild(array $array)
private static function xmlChild(array $array): string
{
$string = '';
foreach ($array as $key => $value) {
@@ -55,67 +55,45 @@ class Help extends Miniprogarampage
/**
* @param $xml
* @return mixed
* @return array|null
*/
public static function toArray($xml)
public static function toArray($xml): ?array
{
if (is_array($xml)) {
return $xml;
}
/* $matchQoute = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';*/
// if (!preg_match($matchQoute, $xml)) {
// return self::jsonToArray($xml);
// }
if (!is_null($data = json_decode($xml, true))) {
return $data;
}
try {
$data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($data !== false) {
$data = json_decode(json_encode($data), TRUE);
} else {
$data = $xml;
}
} catch (\Exception $exception) {
$data = $xml;
} finally {
return $data;
if (is_array($xml)) return $xml;
if (str_starts_with('<', $xml) && str_ends_with('>', $xml)) {
return self::xmlToArray($xml);
} else {
return self::jsonToArray($xml);
}
}
public static function jsonToArray($xml)
/**
* @param string $xml
* @return array|null
*/
public static function jsonToArray(string $xml): ?array
{
$_xml = json_decode($xml, true);
if (is_null($_xml)) {
return $xml;
}
return $_xml;
return json_decode($xml, true);
}
/**
* @param $xml
* @return mixed
* @param string $xml
* @return array|null
*/
public static function xmlToArray($xml)
public static function xmlToArray(string $xml): ?array
{
if (is_array($xml)) {
return $xml;
}
if (($data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) !== false) {
return json_decode(json_encode($data), TRUE);
}
if (!is_null($json = json_decode($xml, TRUE))) {
return $json;
}
return $xml;
return null;
}
/**
* @param mixed $json
* @return false|mixed|string
* @return string
*/
public static function toJson($json)
public static function toJson(mixed $json): string
{
if (is_object($json)) {
$json = get_object_vars($json);
@@ -141,7 +119,7 @@ class Help extends Miniprogarampage
*
* 随机字符串
*/
public static function random($length = 20)
public static function random(int $length = 20): string
{
$res = [];
$str = 'abcdefghijklmnopqrstuvwxyz';
@@ -151,7 +129,7 @@ class Help extends Miniprogarampage
if (empty($rand)) {
$rand = substr($str, strlen($str) - 3, 1);
}
array_push($res, $rand);
$res[] = $rand;
}
return implode($res);
@@ -163,7 +141,7 @@ class Help extends Miniprogarampage
* @param $type
* @return string
*/
public static function sign(array $array, $key, $type)
public static function sign(array $array, $key, $type): string
{
ksort($array);
$buff = "";
File diff suppressed because it is too large Load Diff
-221
View File
@@ -1,221 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/26 0026
* Time: 10:23
*/
namespace wchat\common;
use wchat\common\Config;
abstract class Miniprogarampage implements Progaram
{
/** @var Config */
protected $config;
/** @var Miniprogarampage $instance */
protected static $instance = null;
/** @var HttpClient */
protected $request = null;
protected $errorCode = 0;
protected $errorMsg = '';
/**
* @param $message
* @param int $code
* @return Result
*/
protected function sendError($message, $code = 500)
{
return new Result(['code' => $code, 'message' => $message]);
}
/**
* @param $code
*/
public function setErrorCode($code)
{
$this->errorCode = $code;
}
/**
* @param $message
*/
public function setErrorMessage($message)
{
$this->errorMsg = $message;
}
/**
* @return int
*/
public function getErrorCode()
{
return $this->errorCode;
}
/**
* @return string
*/
public function getErrorMessage()
{
return $this->errorMsg;
}
/**
* Miniprogarampage constructor.
*/
public function __construct()
{
}
/**
* @param Config $config
* @return mixed
*/
public static function getInstance(Config $config)
{
if (static::$instance === null) {
static::$instance = new static();
}
static::$instance->config = $config;
static::$instance->request = HttpClient::NewRequest();
static::$instance->request->setAgent($config->getAgent());
static::$instance->request->setUseSwoole($config->isUsrSwoole());
$request = static::$instance->request;
if ($request->getIsSSL()) {
$request->addHeader('ssl_cert_file', $config->getSslCert());
$request->addHeader('ssl_key_file', $config->getSslKey());
}
return static::$instance;
}
/**
* @param Config $config
* @return $this
*/
public function initConfig($config)
{
$this->config = $config;
$this->request = HttpClient::NewRequest();
$this->request->setIsSSL(true);
$this->request->setData(null);
$this->request->setUseSwoole($config->isUsrSwoole());
if (!empty($config->getAgent())) {
$this->request->setAgent($config->getAgent());
}
if (!empty($config->getSslCert())) {
$this->request->setSslCertFile($config->getSslCert());
}
if (!empty($config->getSslKey())) {
$this->request->setSslKeyFile($config->getSslKey());
}
if (!empty($config->getSslCa())) {
$this->request->setCa($config->getSslCa());
}
return $this;
}
/**
* @return \wchat\common\Config
*/
public function getConfig()
{
return $this->config;
}
/**
* @return bool|mixed|string
* @throws \Exception
*/
protected function getAccessToken()
{
$access = $this->config->getAccessToken();
if (!empty($access)) {
return $access;
}
$this->request->setMethod(HttpClient::GET);
$data = $this->request->get('/cgi-bin/token', [
'grant_type' => 'client_credential',
'appid' => $this->config->getAppid(),
'secret' => $this->config->getAppsecret(),
]);
if (!$data->isResultsOK()) {
throw new \Exception($data->getMessage());
}
$access = $data->getData('access_token');
$this->config->setAccessToken($access);
return $access;
}
/**
* @param $data
* @param $body
* @return mixed
* @throws \Exception
*/
protected function buildResult($data, $body = NULL)
{
$data = $this->checkSign($data);
if (!$data) {
$return['code'] = -1;
$return['message'] = '签名错误.';
} else {
if (isset($data['return_code'])) {
if ($data['return_code'] == 'FAIL') {
$return['code'] = -1;
$return['message'] = $data['return_msg'];
} else {
$return['code'] = 0;
$return['data'] = $data;
$return['data']['postBody'] = $body;
}
} else {
if ($data['errcode'] == 'FAIL') {
$return['code'] = -1;
$return['message'] = $data['errmsg'];
} else {
$return['code'] = 0;
$return['data'] = $data;
$return['data']['postBody'] = $body;
}
}
}
return $return;
}
/**
* @param $result
* @return mixed
* @throws \Exception
*/
protected function checkSign($result)
{
$data = Help::toArray($result);
if (!isset($data['sign'])) {
return $data;
}
$sign = $data['sign'];
unset($data['sign']);
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$_sign = Help::sign($data, $key, $sign_type);
if ($sign != $_sign) {
return FALSE;
}
return $data;
}
}
+115
View File
@@ -0,0 +1,115 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/26 0026
* Time: 10:23
*/
namespace wchat\common;
use wchat\common\Config;
abstract class Multiprogramming implements Progaram
{
/** @var Config */
protected Config $config;
protected static ?Multiprogramming $instance = null;
protected ?HttpClient $request = null;
protected int $errorCode = 0;
protected string $errorMsg = '';
/**
* @param $message
* @param int $code
* @return Result
*/
protected function sendError($message, int $code = 500): Result
{
return new Result(code: $code, message: $message);
}
/**
* @param $code
*/
public function setErrorCode($code)
{
$this->errorCode = $code;
}
/**
* @param $message
*/
public function setErrorMessage($message)
{
$this->errorMsg = $message;
}
/**
* @return int
*/
public function getErrorCode(): int
{
return $this->errorCode;
}
/**
* @return string
*/
public function getErrorMessage(): string
{
return $this->errorMsg;
}
/**
* @param \wchat\common\Config $config
* @return void
*/
public function setConfig(Config $config): void
{
$this->config = $config;
}
/**
* @return \wchat\common\Config
*/
public function getConfig(): \wchat\common\Config
{
return $this->config;
}
/**
* @param $result
* @return array|bool
* @throws \Exception
*/
protected function checkSign($result): array|bool
{
$data = Help::toArray($result);
if (!isset($data['sign'])) {
return $data;
}
$sign = $data['sign'];
unset($data['sign']);
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$_sign = Help::sign($data, $key, $sign_type);
if ($sign != $_sign) {
return FALSE;
}
return $data;
}
}
+32 -66
View File
@@ -2,6 +2,8 @@
namespace wchat\common;
use JetBrains\PhpStorm\ArrayShape;
/**
* Class Result
*
@@ -14,55 +16,38 @@ namespace wchat\common;
*/
class Result
{
public $code;
public $message;
public $count = 0;
public $data;
public $header;
private $startTime;
private $requestTime;
private $runTime;
public function __construct(array $data)
private ?int $startTime;
private ?int $requestTime;
private ?int $runTime;
/**
* @param int $code
* @param string $message
* @param int $count
* @param mixed $data
* @param array $header
*/
public function __construct(public int $code, public string $message = '', public int $count = 0, public mixed $data = [], public array $header = [],)
{
foreach ($data as $key => $val) {
$this->$key = $val;
}
$this->header = $this->reloadResponse($this->header);
}
/**
* @param $header
*
* @return array
* @param $name
* @return mixed
*/
private function reloadResponse($header)
{
$data = [];
if (is_array($header)) {
return $header;
}
$load = explode("\n", $header);
if (!empty($load) && is_array($load)) {
foreach ($load as $key => $val) {
if (empty($val)) continue;
$ex = explode(': ', $val);
if (!empty($ex[0]) && !empty($ex[1])) {
$data[trim($ex[0])] = trim($ex[1]);
}
}
}
return $data;
}
public function __get($name)
{
return $this->$name;
}
/**
* @param $name
* @param $value
* @return $this
*/
public function __set($name, $value)
{
$this->$name = $value;
@@ -73,7 +58,8 @@ class Result
/**
* @return array
*/
public function getTime()
#[ArrayShape(['startTime' => "int|null", 'requestTime' => "int|null", 'runTime' => "int|null"])]
public function getTime(): array
{
return [
'startTime' => $this->startTime,
@@ -88,7 +74,7 @@ class Result
* @return $this
* @throws \Exception
*/
public function setAttr($key, $data)
public function setAttr($key, $data): static
{
if (!property_exists($this, $key)) {
throw new \Exception('未查找到相应对象属性');
@@ -100,7 +86,7 @@ class Result
/**
* @return bool
*/
public function isResultsOK()
public function isResultsOK(): bool
{
if (!is_numeric($this->code)) {
return false;
@@ -108,32 +94,12 @@ class Result
return $this->code == 0;
}
/**
* @param array $headers
* 批量设置返回头
*/
public function setHeaders(array $headers)
{
foreach ($headers as $key => $val) {
$this->setHeader($key, $val);
}
}
/**
* @param $key
* @param $val
* 设置返回头
*/
public function setHeader($key, $val)
{
header($key . ':' . $val);
}
/**
* @param $name
* @param string $name
* @return mixed
*/
public function getData($name = '')
public function getData(string $name = ''): mixed
{
if (!$this->isResultsOK()) {
return $this->data;
@@ -149,7 +115,7 @@ class Result
* @param $data
* @return $this
*/
public function append($key, $data)
public function append($key, $data): static
{
if (!is_array($this->data)) {
$this->data = ['origin' => $this->data];
@@ -159,17 +125,17 @@ class Result
}
/**
* @return mixed
* @return string
*/
public function getMessage()
public function getMessage(): string
{
return $this->message;
}
/**
* @return mixed
* @return int
*/
public function getCode()
public function getCode(): int
{
return $this->code;
}