Model.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace addons\WangDianTong\common\expand\sdk\wangdian;
  3. class Model
  4. {
  5. /**
  6. * @var array
  7. */
  8. protected $data = [];
  9. protected $sort_key = [];
  10. protected $ret_data = [];
  11. protected $sort_reset_key = true;
  12. public function __set($name, $value)
  13. {
  14. $this->data[$name] = $value;
  15. }
  16. public function __get($name)
  17. {
  18. return $this->data[$name] ?? null;
  19. }
  20. public function __isset($name)
  21. {
  22. return isset($this->data[$name]);
  23. }
  24. /**
  25. * @return array
  26. */
  27. public function getData(): array
  28. {
  29. return $this->data;
  30. }
  31. /**
  32. * @return array
  33. */
  34. public function toArray(): array
  35. {
  36. foreach ($this->data as $key => $value) {
  37. if ($value instanceof Model) {
  38. $this->ret_data[$key] = $value->toArray();
  39. } else {
  40. $this->ret_data[$key] = $value;
  41. }
  42. }
  43. return $this->ret_data;
  44. }
  45. /**
  46. * @return array
  47. * @throws \Exception
  48. */
  49. public function sort(): array
  50. {
  51. $data = [];
  52. foreach ($this->sort_key as $item) {
  53. if (!empty($this->ret_data[$item])) {
  54. if (!$this->sort_reset_key) {
  55. $data[$item] = $this->ret_data[$item];
  56. } else {
  57. $data[] = $this->ret_data[$item];
  58. }
  59. }
  60. }
  61. return $data;
  62. }
  63. }