Files
kiri-core/HttpServer/Client/Result.php
T

173 lines
2.5 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Client;
use HttpServer\Application;
use Exception;
/**
* Class Result
*
* @package app\components
*
* @property $code
* @property $message
* @property $count
* @property $data
*/
2020-09-23 16:24:27 +08:00
class Result
2020-08-31 01:27:08 +08:00
{
public $code;
public $message;
2020-10-29 18:17:25 +08:00
public int $count = 0;
public array|string $data;
public array $header;
public int $httpStatus = 200;
public int $startTime = 0;
public int $requestTime = 0;
public float $runTime = 0;
2020-08-31 01:27:08 +08:00
/**
* Result constructor.
* @param array $data
*/
2020-09-23 16:24:27 +08:00
public function __construct(array $data)
2020-08-31 01:27:08 +08:00
{
2020-09-23 16:24:27 +08:00
$this->setAssignment($data);
}
2020-08-31 01:27:08 +08:00
2020-09-23 16:24:27 +08:00
/**
* @param $data
* @return $this
*/
public function setAssignment($data)
{
2020-08-31 01:27:08 +08:00
foreach ($data as $key => $val) {
2020-09-23 16:24:27 +08:00
if (!property_exists($this, $key)) {
continue;
}
2020-08-31 01:27:08 +08:00
$this->$key = $val;
}
2020-09-23 16:24:27 +08:00
return $this;
2020-08-31 01:27:08 +08:00
}
2020-09-23 16:24:27 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @return mixed|null
*/
public function __get($name)
{
return $this->$name;
}
/**
* @param $name
* @param $value
* @return $this|void
*/
public function __set($name, $value)
{
$this->$name = $value;
return $this;
}
/**
* @return array
*/
public function getHeaders()
{
$_tmp = [];
if (!is_array($this->header)) {
return $_tmp;
}
foreach ($this->header as $key => $val) {
if ($key == 0) {
$_tmp['pro'] = $val;
} else {
$trim = explode(': ', $val);
$_tmp[strtolower($trim[0])] = $trim[1];
}
}
return $_tmp;
}
/**
* @return array
*/
public function getTime()
{
return [
'startTime' => $this->startTime,
'requestTime' => $this->requestTime,
'runTime' => $this->runTime,
];
}
/**
* @param $key
* @param $data
* @return $this
* @throws Exception
*/
public function setAttr($key, $data)
{
if (!property_exists($this, $key)) {
throw new Exception('未查找到相应对象属性');
}
$this->$key = $data;
return $this;
}
/**
* @param int $status
* @return bool
*/
public function isResultsOK($status = 0)
{
if (!$this->httpIsOk()) {
return false;
}
return $this->code === $status;
}
/**
* @return bool
*/
public function httpIsOk()
{
2020-09-11 19:43:19 +08:00
return in_array($this->httpStatus, [100, 101, 200, 201, 202, 203, 204, 205, 206]);
2020-08-31 01:27:08 +08:00
}
/**
* @return mixed
*/
public function getResponse()
{
return $this->data;
}
/**
* @return mixed
*/
public function getMessage()
{
return $this->message;
}
/**
* @return mixed
*/
public function getCode()
{
return $this->code;
}
}