123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace app\common\base\error\model;
- abstract class BaseErrorModel {
- protected $code = 0;
- protected $msg = '';
- protected $error = null;
- protected $data = null;
- public function __construct(int $code = 0,string $msg = '', $data = null, $error = null,bool $single = true) {
- $this->beforeInit($code, $msg, $data , $error, $single);
- $this->zskkInit($code, $msg, $data , $error, $single );
- $this->afterInit($code, $msg, $data , $error, $single);
- }
- protected function beforeInit(int $code = 0,string $msg = '', $data = null, $error = null,bool $single = true) { }
- protected function afterInit(int $code = 0,string $msg = '', $data = null, $error = null,bool $single = true) { }
- protected function zskkInit(int $code, string $msg, $data, $error, bool $single) {
- if($single) {
- $this->defaultInit();
- } else {
- $this->customInit($code, $msg, $data , $error);
- }
- }
- private function defaultInit() {
- $this->setResponse($this->code, $this->msg, $this->data , $this->error);
- }
- private function customInit(int $code, string $msg, $data, $error) {
- $this->setResponse($code, $msg, $data , $error);
- }
- public function setResponse($code = 0, $msg = '', $data = null, $error = null) {
- $this->setCode($code);
- $this->setMsg($msg);
- $this->setData($data);
- $this->setError($error);
- }
- public function getResponse() {
- return [
- "code" => $this->getCode(),
- "msg" => $this->getMsg(),
- "data" => $this->getData(),
- "error" => $this->getError(),
- "timestamp" => $this->getTimestamp()
- ];
- }
- private function getTimestamp() {
- return time();
- }
- /**
- * @return int
- */
- public function getCode(): int
- {
- return $this->code;
- }
- /**
- * @param int $code
- */
- public function setCode(int $code)
- {
- $this->code = $code;
- return $this;
- }
- /**
- * @return string
- */
- public function getMsg(): string
- {
- return $this->msg;
- }
- /**
- * @param string $msg
- */
- public function setMsg(string $msg)
- {
- $this->msg = $msg;
- return $this;
- }
- /**
- * @return null
- */
- public function getError()
- {
- return $this->error;
- }
- /**
- * @param null $error
- */
- public function setError($error)
- {
- $this->error = $error;
- return $this;
- }
- /**
- * @return null
- */
- public function getData()
- {
- return $this->data;
- }
- /**
- * @param null $data
- */
- public function setData($data)
- {
- $this->data = $data;
- return $this;
- }
- }
|