icingaweb2-module-businessp.../library/Businessprocess/Html/Html.php

121 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Businessprocess\Html;
use Icinga\Exception\ProgrammingError;
class Html implements Renderable
{
protected $contentSeparator = '';
/**
* @var Renderable[]
*/
private $content = array();
public static function escape($any)
{
return Util::wantHtml($any);
}
/**
* @param Renderable $element
* @return $this
*/
public function add(Renderable $element)
{
$this->content[] = $element;
return $this;
}
/**
* @param Renderable|array|string $content
* @return $this
*/
public function setContent($content)
{
$this->content = array(
static::escape($content)
);
return $this;
}
/**
* @param Renderable|array|string $content
* @return $this
*/
public function addContent($content)
{
$this->content[] = static::escape($content);
return $this;
}
/**
* return Html
*/
public function getContent()
{
if ($this->content === null) {
$this->content = array(new Html());
}
return $this->content;
}
public function hasContent()
{
if ($this->content === null) {
return false;
}
// TODO: unfinished
// return $this->content->isEmpty();
return true;
}
/**
* @param $separator
* @return $this
*/
public function setSeparator($separator)
{
$this->contentSeparator = $separator;
return $this;
}
/**
* @inheritdoc
*/
public function render()
{
$html = array();
foreach ($this->content as $element) {
if (is_string($element)) {
var_dump($this->content);
}
$html[] = $element->render();
}
return implode($this->contentSeparator, $html);
}
public static function element($name, $attributes = null)
{
// TODO: This might be anything here, add a better check
if (! ctype_alnum($name)) {
throw new ProgrammingError('Invalid element requested');
}
$class = __NAMESPACE__ . '\\' . $name;
/** @var Element $element */
$element = new $class();
if ($attributes !== null) {
$element->setAttributes($attributes);
}
return $element;
}
}