add clear
This commit is contained in:
+32
-66
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user