icingaweb2-module-director/library/vendor/ipl/Html/BaseHtmlElement.php

212 lines
4.5 KiB
PHP
Raw Normal View History

2018-05-05 01:04:25 +02:00
<?php
namespace dipl\Html;
abstract class BaseHtmlElement extends HtmlDocument
{
/** @var array You may want to set default attributes when extending this class */
protected $defaultAttributes;
/** @var Attributes */
protected $attributes;
/** @var string */
protected $tag;
protected $hasBeenAssembled = false;
protected static $voidElements = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr'
];
protected $isVoid;
/**
* @return Attributes
2018-05-06 09:08:37 +02:00
* @throws \Icinga\Exception\IcingaException
* @throws \Icinga\Exception\ProgrammingError
2018-05-05 01:04:25 +02:00
*/
public function getAttributes()
{
if ($this->attributes === null) {
$default = $this->getDefaultAttributes();
if (empty($default)) {
$this->attributes = new Attributes();
} else {
$this->attributes = Attributes::wantAttributes($default);
}
}
return $this->attributes;
}
/**
* @param Attributes|array|null $attributes
* @return $this
2018-05-05 18:29:17 +02:00
* @throws \Icinga\Exception\IcingaException
2018-05-05 01:04:25 +02:00
*/
public function setAttributes($attributes)
{
$this->attributes = Attributes::wantAttributes($attributes);
return $this;
}
2018-05-05 18:29:17 +02:00
/**
2018-05-06 09:08:37 +02:00
* @param string $key
* @param mixed $value
2018-05-05 18:29:17 +02:00
* @return $this
* @throws \Icinga\Exception\ProgrammingError
2018-05-06 09:08:37 +02:00
* @throws \Icinga\Exception\IcingaException
2018-05-05 18:29:17 +02:00
*/
2018-05-05 01:04:25 +02:00
public function setAttribute($key, $value)
{
$this->getAttributes()->set($key, $value);
return $this;
}
/**
* @param Attributes|array|null $attributes
* @return $this
2018-05-05 18:29:17 +02:00
* @throws \Icinga\Exception\ProgrammingError
2018-05-05 01:04:25 +02:00
*/
public function addAttributes($attributes)
{
$this->getAttributes()->add($attributes);
return $this;
}
2018-05-05 18:29:17 +02:00
/**
* @return array
*/
2018-05-05 01:04:25 +02:00
public function getDefaultAttributes()
{
return $this->defaultAttributes;
}
public function setTag($tag)
{
$this->tag = $tag;
return $this;
}
public function getTag()
{
return $this->tag;
}
public function renderContent()
{
return parent::render();
}
protected function assemble()
{
}
2018-05-05 18:29:17 +02:00
/**
* @param array|ValidHtml|string $content
* @return $this
2018-05-06 09:08:37 +02:00
* @throws \Icinga\Exception\IcingaException
2018-05-05 18:29:17 +02:00
*/
2018-05-05 01:04:25 +02:00
public function add($content)
{
if (! $this->hasBeenAssembled) {
$this->hasBeenAssembled = true;
$this->assemble();
}
2018-05-06 09:08:37 +02:00
parent::add($content);
return $this;
2018-05-05 01:04:25 +02:00
}
/**
* @return string
2018-05-06 09:08:37 +02:00
* @throws \Icinga\Exception\ProgrammingError
2018-05-05 01:04:25 +02:00
*/
public function render()
{
$tag = $this->getTag();
if (! $this->hasBeenAssembled) {
$this->hasBeenAssembled = true;
$this->assemble();
}
$content = $this->renderContent();
if (strlen($content) || $this->wantsClosingTag()) {
return sprintf(
'<%s%s>%s</%s>',
$tag,
$this->renderAttributes(),
$content,
$tag
);
} else {
return sprintf(
'<%s%s />',
$tag,
$this->renderAttributes()
);
}
}
2018-05-06 09:08:37 +02:00
/**
* @return string
* @throws \Icinga\Exception\ProgrammingError
*/
2018-05-05 01:04:25 +02:00
public function renderAttributes()
{
if ($this->attributes === null && empty($this->defaultAttributes)) {
return '';
} else {
return $this->getAttributes()->render();
}
}
public function wantsClosingTag()
{
// TODO: There is more. SVG and MathML namespaces
return ! $this->isVoidElement();
}
public function isVoidElement()
{
if ($this->isVoid === null) {
$this->isVoid = in_array($this->tag, self::$voidElements);
}
return $this->isVoid;
}
public function setVoid($void = true)
{
$this->isVoid = $void;
return $this;
}
/**
* Whether the given something can be rendered
*
* @param mixed $any
* @return bool
*/
protected function canBeRendered($any)
{
return is_string($any) || is_int($any) || is_null($any);
}
}