private function getAllNodes(): array
{
$nodes = $this->modelClass::find()
->where(['active' => 1, 'visible' => 1])
->orderBy(['lft' => SORT_ASC])
->all();
$menuArray = array();
foreach($nodes as $node) {
$menuArray[$node->id] = $node->attributes;
}
return $menuArray;
}
public function treeArray(): array
{
$nodes = $this->getAllNodes();
$result = array();
$stack = array();
foreach ($nodes as $node) {
$item = $node;
$item['children'] = array();
$level = count($stack);
while($level > 0 && $stack[$level - 1]['lvl'] >= $item['lvl']) {
array_pop($stack);
$level--;
}
if ($level === 0) {
$i = count($result);
$result[$i] = $item;
$stack[] = & $result[$i];
} else {
$i = count($stack[$level - 1]['children']);
$stack[$level - 1]['children'][$i] = $item;
$stack[] = & $stack[$level - 1]['children'][$i];
}
}
return $result;
}