lib: Add a not yet customizable node renderer
This commit is contained in:
parent
ce0aee5e41
commit
6c8d35c667
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue