Created
August 20, 2010 10:52
-
-
Save anonymous/540059 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* All nodes should be added from a list | |
* where the nodes are ordered by parent then id | |
* | |
* MikeC - My favourite object | |
*/ | |
class Gravitywell_Tree | |
{ | |
/** | |
* | |
* this node's id | |
*/ | |
public $id; | |
/** | |
* | |
* parent id | |
*/ | |
public $parent; | |
/** | |
* | |
* array of hops | |
*/ | |
public $hops; | |
/** | |
* | |
* This object's data | |
*/ | |
public $data = null; | |
/** | |
* | |
* Object Reference | |
*/ | |
protected $_arrRefs = array(); | |
/** | |
* | |
* Child nodes | |
*/ | |
protected $_childNodes = array(); | |
/** | |
* | |
* Construct | |
*/ | |
public function __construct($id = 0, $parent = 0) | |
{ | |
$this->id = $id; | |
$this->parent = $parent; | |
$this->_arrRefs[$id] = &$this; | |
// Add the root hop | |
$this->hops = array(0 => $id); | |
} | |
/** | |
* Add a child to a parent | |
* | |
*/ | |
public function addChild($parent, $id, $data = null) | |
{ | |
if($obj = $this->findParent($parent)) { | |
// Rerun this class for the child | |
$newObj = new self($id, $parent); | |
// do hops | |
$newObj->hops = $obj->hops; | |
$newObj->hops[] = $id; | |
// add data | |
$newObj->data = $data; | |
// Add our child node | |
$obj->_childNodes[$id] = $newObj; | |
// Create the reference to it | |
$this->_arrRefs[$id] = &$newObj; | |
} | |
else { | |
// Could not find the parent | |
throw new Exception('Gravitywell_Tree::addChild: Unknown parent id'); | |
} | |
} | |
/** | |
* hasChildren | |
* | |
* does this obj node have children | |
*/ | |
public function hasChildren() | |
{ | |
return (bool) count($this->_childNodes); | |
} | |
/** | |
* function getChildren | |
* @param $obj | |
* @return array of Gravitywell_Tree objects | |
*/ | |
public function getChildren($obj = null) { | |
if (!$obj) { | |
$obj = $this; | |
} | |
if (!$obj->hasChildren()) { | |
return false; | |
} | |
return $obj->_childNodes; | |
} | |
/** | |
* return the array of hops from here to root node | |
*/ | |
public function getHops() | |
{ | |
return $this->hops; | |
} | |
/** | |
* findNode | |
* | |
* @param int $id | |
* @return object this|false | |
*/ | |
public function findNode($id) | |
{ | |
return $this->findParent($id); | |
} | |
/** | |
* findChild | |
* | |
* @param int $id | |
* @return object this|false | |
**/ | |
public function findChild($id) | |
{ | |
return $this->findParent($id); | |
} | |
/** | |
* FindParent | |
* | |
* Finds the parent for a child/parent/node who cares? | |
* | |
* @param int $id | |
* @return object this|false | |
*/ | |
public function findParent($id) | |
{ | |
if(isset ($this->_arrRefs[$id])) { | |
return $this->_arrRefs[$id]; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* | |
* set this nodes data property | |
*/ | |
public function setData($data) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* | |
* returns the nodes data property | |
*/ | |
public function getData() | |
{ | |
return $this->data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment