FleetCart/Modules/Menu/Services/MenuItemOrdener.php

44 lines
1.0 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Modules\Menu\Services;
use Modules\Menu\Entities\MenuItem;
class MenuItemOrdener
{
/**
* Order the menu items.
*
* @param array $rootItem
* @return void
*/
public function order(array $rootItem)
{
$this->orderChildren($rootItem['id'], $rootItem['children']);
}
/**
* Order child menu items recursively.
*
* @param int $parentId
* @param array $childItems
* @return void
*/
private function orderChildren($parentId, array $childItems)
{
foreach ($childItems as $position => $childItem) {
$menuItem = MenuItem::find($childItem['id']);
if (is_null($menuItem)) {
continue;
}
$menuItem->update(['parent_id' => $parentId, 'position' => $position]);
if (isset($childItem['children'])) {
$this->orderChildren($childItem['id'], $childItem['children']);
}
}
}
}