File: D:/HostingSpaces/SBogers100/femmassebv.com/workbench/komma/kms/src/Komma/Kms/Core/Tree/Tree.php
<?php
namespace Komma\Kms\Core\Tree;
use Carbon\Carbon;
class Tree
{
protected $index = [];
protected $tree = [];
protected $nodes;
/**
* @param array $data
* @return array
*/
public function make(array $data)
{
$this->nodes = $data;
$root = $this->createRoot();
foreach($this->nodes as $key => &$node)
{
$this->tree[] = $this->findChildren($node, $key);
if($this->nodeIsLastLeaf($node,$root))
{
return $this;
}
};
return $this;
}
public function getTree()
{
return $this->tree;
}
public function getIndex()
{
return $this->index;
}
/**
* @param $categoryId
* @param $entityTree
* @return bool|Entity
*/
public function getEntityById($id)
{
if(isset($id) && isset($this->getIndex()[$id])) return $this->getIndex()[$id];
return $this->tree[0];
}
/**
* @param $entityId
* @param $entityTree
* @return int
*/
public function findNodeLevel($entityId, $entityTree)
{
$level = 0;
if(isset($entityTree['index']) && $index = $entityTree['index'])
{
$level++;
// Get rid of the index
unset($entityTree['index']);
$entity = $index[$entityId];
while($entity = $entity->parent)
{
$level++;
}
}
return $level;
}
/**
* @return object
*/
protected function createRoot()
{
// Create ghost root
$largestRgt = 0;
foreach($this->nodes as $node)
{
if($node->rgt > $largestRgt) $largestRgt = $node->rgt;
}
return (object) ['rgt' => $largestRgt + 1];
}
/**
* @param TreeNode $node
* @param $key
* @param null $parent
* @return array
*/
protected function findChildren($node, $key, &$parent = null)
{
$entity = new TreeNode($node, $parent);
$this->index[$node->id] = $entity;
for($i = $key+1; $i < count($this->nodes); $i++ )
{
if(isset($this->index[$this->nodes[$i]->id])){
continue; // don't process if node is already processed
}
$childNode = $this->nodes[$i];
if($this->nodeHasChildren($childNode))
{
$this->index[$childNode->id] = $this->findChildren($childNode, $i, $entity);
$entity->addChildNode($this->index[$childNode->id]);
}
else
{
$this->index[$childNode->id] = new TreeNode($childNode, $entity);
$entity->addChildNode($this->index[$childNode->id]);
}
if($this->nodeIsLastLeaf($childNode,$node))
{
return $entity;
}
}
return $entity;
}
/**
* @param $node
* @param $parent
* @return bool
*/
protected function nodeIsLastLeaf($node, $parent)
{
if(isset($parent->rgt) && $node->lft)
{
return $node->rgt == ($parent->rgt - 1);
}
return false;
}
/**
* @param $node
* @return bool
*/
protected function nodeHasChildren($node)
{
if(isset($node->lft) && $node->rgt)
{
return ($node->lft + 1) != $node->rgt;
}
return false;
}
public function removeFromIndex($id){
if(isset($this->index[$id])){
foreach($this->index[$id]->getChildren() as $child)
{
$this->removeFromIndex($child->node->id);
}
unset($this->index[$id]);
}
}
/**
* Sort top-layer of tree by published at
*
* @return $this|bool
*/
public function sortByPublished()
{
// Return false if we don't have a root node
if( ! isset($this->tree[0])) return false;
// Get immediate children
$children = $this->tree[0]->getChildren();
// Sort by published_at with usort
usort($children, function($a, $b)
{
// Parse to datetime
$dateTimeA = Carbon::parse($a->node->published_at);
$dateTimeB = Carbon::parse($b->node->published_at);
// Compare timestamps
return $dateTimeB->timestamp - $dateTimeA->timestamp;
});
// Set children in reversed order
$this->tree[0]->setChildren($children);
return $this;
}
}