-
-
Save vrkansagara/a83b0761b0c9a84a1baefcff62c7ff5b 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 | |
setup(); | |
$t = new Tree('top', 'T'); | |
$t->append(new TreeNode('one', 1)); | |
$t->append($x = new TreeNode('two', 2)); | |
$t->append(new TreeNode('three', 3)); | |
$t->append(new TreeNode('foo', 'foo')); | |
$x->append(new TreeNode('a', 'a')); | |
$x->append(new TreeNode('b', 'b')); | |
//$r = new RecursiveIteratorIterator($t, RecursiveIteratorIterator::CHILD_FIRST); | |
// | |
//foreach ($r as $s) { | |
// | |
// | |
//} | |
$renderer = new TreeRendererXML($t); | |
$renderer->render(); | |
function setup() { | |
abstract class TreeContainer implements RecursiveIterator, Countable | |
{ | |
protected $_name = ''; | |
protected $_branches = array(); | |
public function __construct($name, $value = null) | |
{ | |
$this->setName($name); | |
if ($value) { | |
$this->setValue($value); | |
} | |
} | |
public function setName($name) | |
{ | |
$this->_name = $name; | |
} | |
public function getName() | |
{ | |
return $this->_name; | |
} | |
public function getValue() | |
{ | |
return $this->_value; | |
} | |
public function setValue($value) | |
{ | |
$this->_value = $value; | |
} | |
public function append(TreeContainer $node) | |
{ | |
array_push($this->_branches, $node); | |
} | |
public function current() | |
{ | |
return current($this->_branches); | |
} | |
public function key() | |
{ | |
return key($this->_branches); | |
} | |
public function next() | |
{ | |
return next($this->_branches); | |
} | |
public function rewind() | |
{ | |
return reset($this->_branches); | |
} | |
public function valid() | |
{ | |
return (bool) $this->current(); | |
} | |
public function hasChildren() | |
{ | |
return (count($this->_branches > 0)) ? true : false; | |
} | |
public function getChildren() | |
{ | |
return $this->current(); | |
} | |
public function count() | |
{ | |
return count($this->_branches); | |
} | |
public function __toString() | |
{ | |
return $this->_name; | |
} | |
} | |
class Tree extends TreeContainer | |
{ | |
public function setValue($value) | |
{ | |
if ($value == 'foo') { | |
$this->_value = 'BIG FUCKING FOO'; | |
} else { | |
parent::setValue($value); | |
} | |
} | |
} | |
class TreeNode extends TreeContainer | |
{ | |
} | |
interface Renderer | |
{ | |
public function render(); | |
} | |
class TreeRendererHtml implements Renderer | |
{ | |
protected $_node = null; | |
public function __construct(TreeContainer $treeNode) | |
{ | |
$this->_node = $treeNode; | |
} | |
public function render() | |
{ | |
$rii = new RecursiveIteratorIterator($this->_node, RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($rii as $i) { | |
echo str_repeat(' ', $rii->getDepth()); | |
echo $i->getName() . PHP_EOL; | |
} | |
} | |
} | |
class TreeRendererXML extends TreeRendererHtml | |
{ | |
public function render() | |
{ | |
$rii = new RecursiveIteratorIterator($this->_node, RecursiveIteratorIterator::SELF_FIRST); | |
$xmlWriter = new XMLWriter(); | |
$xmlWriter->openURI('php://stdout'); | |
$xmlWriter->setIndent(true); | |
$xmlWriter->setIndentString(' '); | |
$xmlWriter->startDocument('1.0'); | |
$lastDepth = 0; | |
foreach ($rii as $i) { | |
if ($i->getValue()) { | |
$xmlWriter->writeElement($i->getName(), $i->getValue()); | |
} | |
if ($rii->getDepth() >= $lastDepth) { | |
$xmlWriter->startElement($i->getName()); | |
} elseif ($rii->getDepth() < $lastDeth) { | |
$xmlWRtier->endElement(); | |
} | |
$lastDepth = $rii->getDepth(); | |
} | |
$xmlWriter->endElement(); | |
$xmlWriter->endDocument(); | |
$xmlWriter->flush(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment