public function hydrate($resolver, $decorator, $idField = 'id', $parentIdField = 'parent_id', $parentId = 0)
{
$items = is_callable($resolver) ? $resolver() : $resolver;
if ($items instanceof Collection) {
$items = $items->all();
}
$itemsForThisLevel = array_filter($items, function ($item) use($parentId, $parentIdField) {
return $parentId == (is_object($item) ? isset($item->{$parentIdField}) ? $item->{$parentIdField} : 0 : (isset($item[$parentIdField]) ? $item[$parentIdField] : 0));
});
foreach ($itemsForThisLevel as $item) {
// Let the decorator add the item(s) (and maybe set some attributes)
$decorator($this, $item);
// Grab the newest item
$newestItem = end($this->children);
// If there is an item, add hydrate it
if ($newestItem) {
// Grab the newest itemlist
$newestItemList = $newestItem->getChildren();
// Get the id of the item
$parentId = is_object($item) ? $item->{$idField} : $item[$idField];
// Hydrate the children
$newestItemList->hydrate($items, $decorator, $idField, $parentIdField, $parentId);
}
}
return $this;
}