NavigationOrdener.php 778 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php namespace Modules\Core\Navigation;

use Illuminate\Support\Collection;

class NavigationOrdener
{
    public static function order(Collection $items)
    {
        return $items->sort(
            function ($item1, $item2) {
                $item1 = self::getItem($item1);
                $item2 = self::getItem($item2);

                if ($item1['weight'] > $item2['weight']) {
                    return 1;
                }
                if ($item1['weight'] < $item2['weight']) {
                    return -1;
                }
                return 0;
            }
        );
    }

    /**
     * @param $item
     * @return mixed
     */
    public static function getItem($item)
    {
        return isset($item['weight']) ? $item : $item->first();
    }
33
}