<?php
namespace App\Services\Tree;

use Baum\Extensions\Eloquent\Collection as BaseCollection;

class Collection extends BaseCollection {

  /**
   * @inheritdoc
   * @return \Baum\Extensions\Eloquent\Collection
   */
  public function toHierarchy() {
    $dict = $this->getDictionary();

    // Enforce sorting by $orderColumn setting in Baum\Node instance
    uasort($dict, function($a, $b){
        if ($a->getOrder() == $b->getOrder()) {
            return ($a->getKey() > $b->getKey()) ? 1 : -1;
        } else {
            return ($a->getOrder() > $b->getOrder()) ? 1 : -1;
        }
    });

    return new BaseCollection($this->hierarchical($dict));
  }

}
