2018-07-19 12:10:22 +08:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-05 12:41:49 +08:00
|
|
|
namespace wchat\common;
|
2018-07-19 12:10:22 +08:00
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
use JetBrains\PhpStorm\ArrayShape;
|
|
|
|
|
|
2018-07-19 12:10:22 +08:00
|
|
|
/**
|
|
|
|
|
* Class Result
|
|
|
|
|
*
|
|
|
|
|
* @package app\components
|
|
|
|
|
*
|
|
|
|
|
* @property $code
|
|
|
|
|
* @property $message
|
|
|
|
|
* @property $count
|
|
|
|
|
* @property $data
|
|
|
|
|
*/
|
|
|
|
|
class Result
|
|
|
|
|
{
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
private ?int $startTime;
|
|
|
|
|
private ?int $requestTime;
|
|
|
|
|
private ?int $runTime;
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2018-07-19 12:10:22 +08:00
|
|
|
/**
|
2022-09-09 16:42:55 +08:00
|
|
|
* @param int $code
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param int $count
|
|
|
|
|
* @param mixed $data
|
|
|
|
|
* @param array $header
|
2018-07-19 12:10:22 +08:00
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
public function __construct(public int $code, public string $message = '', public int $count = 0, public mixed $data = [], public array $header = [],)
|
2018-07-19 12:10:22 +08:00
|
|
|
{
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2018-07-19 12:10:22 +08:00
|
|
|
public function __get($name)
|
|
|
|
|
{
|
|
|
|
|
return $this->$name;
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
|
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $value
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2018-07-19 12:10:22 +08:00
|
|
|
public function __set($name, $value)
|
|
|
|
|
{
|
|
|
|
|
$this->$name = $value;
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2018-07-19 12:10:22 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2020-03-31 16:15:01 +08:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
#[ArrayShape(['startTime' => "int|null", 'requestTime' => "int|null", 'runTime' => "int|null"])]
|
|
|
|
|
public function getTime(): array
|
2018-07-19 12:10:22 +08:00
|
|
|
{
|
|
|
|
|
return [
|
2019-11-11 18:14:47 +08:00
|
|
|
'startTime' => $this->startTime,
|
2018-07-19 12:10:22 +08:00
|
|
|
'requestTime' => $this->requestTime,
|
2019-11-11 18:14:47 +08:00
|
|
|
'runTime' => $this->runTime,
|
2018-07-19 12:10:22 +08:00
|
|
|
];
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2018-07-19 12:10:22 +08:00
|
|
|
/**
|
|
|
|
|
* @param $key
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return $this
|
2018-07-19 18:48:36 +08:00
|
|
|
* @throws \Exception
|
2018-07-19 12:10:22 +08:00
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
public function setAttr($key, $data): static
|
2018-07-19 12:10:22 +08:00
|
|
|
{
|
|
|
|
|
if (!property_exists($this, $key)) {
|
|
|
|
|
throw new \Exception('未查找到相应对象属性');
|
|
|
|
|
}
|
|
|
|
|
$this->$key = $data;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2020-03-31 16:15:01 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
public function isResultsOK(): bool
|
2018-07-19 12:10:22 +08:00
|
|
|
{
|
2019-09-04 14:08:01 +08:00
|
|
|
if (!is_numeric($this->code)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-19 12:10:22 +08:00
|
|
|
return $this->code == 0;
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
|
|
|
|
|
2018-07-19 12:10:22 +08:00
|
|
|
/**
|
2022-09-09 16:42:55 +08:00
|
|
|
* @param string $name
|
2018-07-19 12:10:22 +08:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
public function getData(string $name = ''): mixed
|
2018-07-19 12:10:22 +08:00
|
|
|
{
|
|
|
|
|
if (!$this->isResultsOK()) {
|
2020-01-10 19:47:52 +08:00
|
|
|
return $this->data;
|
2018-07-19 12:10:22 +08:00
|
|
|
}
|
|
|
|
|
if (!empty($name) && isset($this->data[$name])) {
|
|
|
|
|
return $this->data[$name];
|
|
|
|
|
}
|
|
|
|
|
return $this->data;
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2018-07-19 12:10:22 +08:00
|
|
|
/**
|
|
|
|
|
* @param $key
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
public function append($key, $data): static
|
2018-07-19 12:10:22 +08:00
|
|
|
{
|
2020-04-30 22:09:08 +08:00
|
|
|
if (!is_array($this->data)) {
|
2020-04-30 22:25:50 +08:00
|
|
|
$this->data = ['origin' => $this->data];
|
2020-04-30 22:09:08 +08:00
|
|
|
}
|
2018-07-19 12:10:22 +08:00
|
|
|
$this->data[$key] = $data;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2019-11-11 18:14:47 +08:00
|
|
|
/**
|
2022-09-09 16:42:55 +08:00
|
|
|
* @return string
|
2019-11-11 18:14:47 +08:00
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
public function getMessage(): string
|
2018-07-19 12:10:22 +08:00
|
|
|
{
|
|
|
|
|
return $this->message;
|
|
|
|
|
}
|
2019-09-04 14:08:01 +08:00
|
|
|
|
2019-11-11 18:14:47 +08:00
|
|
|
/**
|
2022-09-09 16:42:55 +08:00
|
|
|
* @return int
|
2019-11-11 18:14:47 +08:00
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
public function getCode(): int
|
2018-07-19 12:10:22 +08:00
|
|
|
{
|
|
|
|
|
return $this->code;
|
|
|
|
|
}
|
|
|
|
|
}
|