MenuService.php 788 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace common\services;
  3. use common\models\BaseMenu;
  4. use common\util\MenuObject;
  5. class MenuService
  6. {
  7. /**
  8. * @param $info
  9. * @return MenuObject[]
  10. */
  11. public static function getMenuObjData($info): array
  12. {
  13. $data = [];
  14. $objList = [];
  15. foreach ($info as $value) {
  16. /** @var BaseMenu $value */
  17. $obj = new MenuObject($value);
  18. $objList[$obj->getId()] = $obj;
  19. if (is_null($obj->getPid())) {
  20. $data[] = $obj;
  21. }
  22. }
  23. foreach ($objList as $key => $obj) {
  24. if (!is_null($obj->getPid()) && isset($objList[$obj->getPid()])) {
  25. $objList[$obj->getPid()]->addChildrenToTop($obj);
  26. }
  27. }
  28. return $data;
  29. }
  30. }