Error.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace think\swoole\rpc;
  3. class Error implements \JsonSerializable
  4. {
  5. /**
  6. * @var int
  7. */
  8. protected $code = 0;
  9. /**
  10. * @var string
  11. */
  12. protected $message = '';
  13. /**
  14. * @var mixed
  15. */
  16. protected $data;
  17. /**
  18. * @param int $code
  19. * @param string $message
  20. * @param mixed $data
  21. *
  22. * @return Error
  23. */
  24. public static function make(int $code, string $message, $data = null): self
  25. {
  26. $instance = new static();
  27. $instance->code = $code;
  28. $instance->message = $message;
  29. $instance->data = $data;
  30. return $instance;
  31. }
  32. /**
  33. * @return int
  34. */
  35. public function getCode(): int
  36. {
  37. return $this->code;
  38. }
  39. /**
  40. * @return string
  41. */
  42. public function getMessage(): string
  43. {
  44. return $this->message;
  45. }
  46. /**
  47. * @return mixed
  48. */
  49. public function getData()
  50. {
  51. return $this->data;
  52. }
  53. public function jsonSerialize()
  54. {
  55. return [
  56. 'code' => $this->code,
  57. 'message' => $this->message,
  58. 'data' => $this->data,
  59. ];
  60. }
  61. }