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);
|
2018-05-23 08:30:37 +02:00
|
|
|
|
2018-05-05 01:04:25 +02:00
|
|
|
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);
|
2018-05-23 08:30:37 +02:00
|
|
|
|
2018-05-05 01:04:25 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Attributes|array|null $attributes
|
|
|
|
* @return $this
|
2018-05-05 18:29:17 +02:00
|
|
|
* @throws \Icinga\Exception\ProgrammingError
|
2018-05-23 08:30:37 +02:00
|
|
|
* @throws \Icinga\Exception\IcingaException
|
2018-05-05 01:04:25 +02:00
|
|
|
*/
|
|
|
|
public function addAttributes($attributes)
|
|
|
|
{
|
|
|
|
$this->getAttributes()->add($attributes);
|
2018-05-23 08:30:37 +02:00
|
|
|
|
2018-05-05 01:04:25 +02:00
|
|
|
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;
|
2018-05-23 08:30:37 +02:00
|
|
|
|
2018-05-05 01:04:25 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTag()
|
|
|
|
{
|
|
|
|
return $this->tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderContent()
|
|
|
|
{
|
2018-05-23 08:30:37 +02:00
|
|
|
return parent::renderUnwrapped();
|
2018-05-05 01:04:25 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-05-23 08:30:37 +02:00
|
|
|
$this->ensureAssembled();
|
2018-05-05 01:04:25 +02:00
|
|
|
|
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-23 08:30:37 +02:00
|
|
|
* @throws \Icinga\Exception\IcingaException
|
2018-05-05 01:04:25 +02:00
|
|
|
*/
|
2018-05-23 08:30:37 +02:00
|
|
|
public function renderUnwrapped()
|
2018-05-05 01:04:25 +02:00
|
|
|
{
|
2018-05-23 08:30:37 +02:00
|
|
|
$this->ensureAssembled();
|
2018-05-05 01:04:25 +02:00
|
|
|
$tag = $this->getTag();
|
|
|
|
|
|
|
|
$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-23 08:30:37 +02:00
|
|
|
* @throws \Icinga\Exception\IcingaException
|
2018-05-06 09:08:37 +02:00
|
|
|
*/
|
2018-05-05 01:04:25 +02:00
|
|
|
public function renderAttributes()
|
|
|
|
{
|
|
|
|
if ($this->attributes === null && empty($this->defaultAttributes)) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return $this->getAttributes()->render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 08:30:37 +02:00
|
|
|
/**
|
|
|
|
* @param HtmlDocument $document
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function wrap(HtmlDocument $document)
|
|
|
|
{
|
|
|
|
$document->addWrapper($this);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-05-05 01:04:25 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|