You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.4 KiB
PHTML
66 lines
1.4 KiB
PHTML
2 years ago
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Helper;
|
||
|
|
||
|
use App\Constants\ResultCode;
|
||
|
use Hyperf\Utils\Contracts\Arrayable;
|
||
|
|
||
|
class Result implements Arrayable
|
||
|
{
|
||
|
private $code;
|
||
|
private $message;
|
||
|
private $data = [];
|
||
|
|
||
|
public function __construct(int $code = ResultCode::SUCCESS, string $message = '', array $data = [])
|
||
|
{
|
||
|
$this->code = $code;
|
||
|
$this->message = empty($message) ? ResultCode::getMessage($code) : $message;
|
||
|
$this->data = $data;
|
||
|
}
|
||
|
|
||
|
public function getCode(): int
|
||
|
{
|
||
|
return $this->code;
|
||
|
}
|
||
|
|
||
|
public function setCode(int $code)
|
||
|
{
|
||
|
return $this->code = $code;
|
||
|
}
|
||
|
|
||
|
public function getMessage(): string
|
||
|
{
|
||
|
return $this->message;
|
||
|
}
|
||
|
|
||
|
public function setMessage(string $message)
|
||
|
{
|
||
|
return $this->message = empty($message) ? ResultCode::getMessage($this->code) : $message;
|
||
|
}
|
||
|
|
||
|
public function getData(): array
|
||
|
{
|
||
|
return $this->data;
|
||
|
}
|
||
|
|
||
|
public function setData(array $data)
|
||
|
{
|
||
|
return $this->data = $data;
|
||
|
}
|
||
|
|
||
|
public function toArray(): array
|
||
|
{
|
||
|
return [
|
||
|
'code' => $this->code,
|
||
|
'message' => $this->message,
|
||
|
'data' => new \ArrayObject($this->data),
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public function __toString()
|
||
|
{
|
||
|
return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE);
|
||
|
}
|
||
|
}
|