MenuObject.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace common\util;
  3. use common\models\BaseMenu;
  4. use JsonSerializable;
  5. /**
  6. * Class MenuObject
  7. * @package common\util
  8. * @property int $id
  9. * @property int|null $pid
  10. * @property string $name
  11. * @property string $title
  12. * @property array $children
  13. */
  14. class MenuObject implements JsonSerializable
  15. {
  16. private int $id;
  17. private int|null $pid;
  18. private string $name;
  19. private string $title;
  20. /**
  21. * 节点属性
  22. */
  23. private array $children = [];
  24. /**
  25. * @return int
  26. */
  27. public function getId(): int
  28. {
  29. return $this->id;
  30. }
  31. /**
  32. * @param int $id
  33. */
  34. public function setId(int $id)
  35. {
  36. $this->id = $id;
  37. }
  38. /**
  39. * @return int|null
  40. */
  41. public function getPid(): int|null
  42. {
  43. return $this->pid;
  44. }
  45. /**
  46. * @param int|null $pid
  47. */
  48. public function setPid(int|null $pid)
  49. {
  50. $this->pid = $pid;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function getName(): string
  56. {
  57. return $this->name;
  58. }
  59. /**
  60. * @param string $name
  61. */
  62. public function setName(string $name)
  63. {
  64. $this->name = $name;
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getTitle(): string
  70. {
  71. return $this->title;
  72. }
  73. /**
  74. * @param string $title
  75. */
  76. public function setTitle(string $title)
  77. {
  78. $this->title = $title;
  79. }
  80. /**
  81. * @return array
  82. */
  83. public function getChildren(): array
  84. {
  85. return $this->children;
  86. }
  87. /**
  88. * @param array $children
  89. */
  90. public function setChildren(array $children)
  91. {
  92. $this->children = $children;
  93. }
  94. /**
  95. * @param MenuObject $children
  96. */
  97. public function addChildrenToTop(MenuObject $children)
  98. {
  99. array_unshift($this->children, $children);
  100. }
  101. /**
  102. * @param MenuObject $children
  103. */
  104. public function addChildrenToBottom(MenuObject $children)
  105. {
  106. $this->children[] = $children;
  107. }
  108. public function __construct(BaseMenu $menu)
  109. {
  110. $this->id = $menu->id;
  111. $this->pid = $menu->parentid;
  112. $this->name = $menu->url;
  113. $this->title = $menu->name;
  114. }
  115. /**
  116. * json_encode无法转化私有属性,需要使用jsonSerialize自定义转换私有的属性
  117. * @return array
  118. */
  119. public function jsonSerialize(): array
  120. {
  121. return [
  122. "id" => $this->id,
  123. "pid" => $this->pid,
  124. "name" => $this->name,
  125. "title" => $this->title,
  126. "children" => $this->children,
  127. ];
  128. }
  129. }