Created
May 19, 2014 19:02
-
-
Save krusynth/58e8faa0e3f81eee425f to your computer and use it in GitHub Desktop.
Node Tree
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 | |
// TODO: Implement RecursiveIterator | |
class NodeBranch | |
{ | |
private $_content; | |
private $_children = array(); | |
private $_parent; | |
public function __construct(&$node = null) | |
{ | |
if(isset($node)) | |
{ | |
$this->_content =& $node; | |
} | |
} | |
public function get() | |
{ | |
return $this->_content; | |
} | |
public function children() | |
{ | |
return $this->_children; | |
} | |
public function addChild(&$node) | |
{ | |
$node->parent($this); | |
$this->_children[] = &$node; | |
return $node; | |
} | |
public function parent(&$parent = null) | |
{ | |
if(isset($parent)) | |
{ | |
$this->_parent =& $parent; | |
} | |
return $this->_parent; | |
} | |
} | |
class NodeTree | |
{ | |
private $_data; | |
private $_current; | |
private $_depth = 0; | |
public function __construct() | |
{ | |
// print 'NodeTree: New NodeTree'; | |
// print 'NodeTree: Added trunk'; | |
$this->_data = new NodeBranch(); | |
// print 'NodeTree: Setting current'; | |
$this->_current =& $this->_data; | |
// var_dump($_current); | |
} | |
public function current() | |
{ | |
// print 'NodeTree: current'; | |
return $this->_current->get(); | |
} | |
public function up() | |
{ | |
// print 'NodeTree: Asceding node'; | |
$parent = $this->_current->parent(); | |
if(isset($parent)) | |
{ | |
$this->_depth--; | |
$this->_current =& $this->_current->parent(); | |
} | |
} | |
public function addNode(&$node) | |
{ | |
// print 'NodeTree: Adding node'; | |
$branch = new NodeBranch($node); | |
// var_dump($branch); | |
// print 'NodeTree: Adding node as child'; | |
// var_dump($this->_data, $this->_current); | |
$this->_current->addChild($branch); | |
$this->_current =& $branch; | |
$this->_depth++; | |
// print 'NodeTree: new depth ' . $this->_depth; | |
} | |
public function getDepth() | |
{ | |
// print 'NodeTree: getDepth' . $this->_depth; | |
return $this->_depth; | |
} | |
public function rewind() | |
{ | |
// print 'NodeTree: rewind'; | |
$_current =& $_data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment