lib: Add a not yet customizable node renderer

This commit is contained in:
Eric Lippmann 2014-06-06 13:58:40 +02:00
parent ce0aee5e41
commit 6c8d35c667
1 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}
namespace Icinga\Data\Tree;
use Exception;
use RecursiveIteratorIterator;
use RuntimeException;
/**
* A not yet customizable node renderer
*/
class NodeRenderer extends RecursiveIteratorIterator
{
protected $content = array();
public function __construct(NodeInterface $node)
{
parent::__construct($node, RecursiveIteratorIterator::SELF_FIRST);
}
public function beginIteration()
{
$this->content[] = '<ul>';
}
public function endIteration()
{
$this->content[] = '</ul>';
}
public function beginChildren()
{
$this->content[] = '<ul>';
}
public function endChildren()
{
$this->content[] = '</ul>';
}
public function render($callback)
{
if (! is_callable($callback)) {
throw new RuntimeException('Callable expected');
}
foreach ($this as $node) {
try {
$content = call_user_func($callback, $node);
} catch (Exception $e) {
throw new RuntimeException($e);
}
$this->content[] = $content;
}
return implode("\n", $this->content);
}
}