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

158 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2017-10-09 15:23:27 +02:00
namespace dipl\Html;
use InvalidArgumentException;
/**
* Class Html
*
* This is your main utility class when working with ipl\Html
*
* @package ipl\Html
*/
abstract class Html
{
/**
* Create a HTML element from the given tag, attributes and content
*
* This method does not render the HTML element but creates a {@link HtmlElement}
* instance from the given tag, attributes and content
*
* @param string $name The desired HTML tag name
* @param mixed $attributes HTML attributes or content for the element
* @param mixed $content The content of the element if no attributes have been given
*
* @return HtmlElement The created element
*/
public static function tag($name, $attributes = null, $content = null)
{
if ($attributes instanceof ValidHtml || is_string($attributes)) {
$content = $attributes;
$attributes = null;
} elseif (is_array($attributes)) {
reset($attributes);
if (is_int(key($attributes))) {
$content = $attributes;
$attributes = null;
}
}
2018-05-05 00:18:45 +02:00
return new HtmlElement($name, $attributes, $content);
}
2018-05-05 00:18:45 +02:00
/**
* Convert special characters to HTML5 entities using the UTF-8 character
* set for encoding
*
* This method internally uses {@link htmlspecialchars} with the following
* flags:
*
* * Single quotes are not escaped (ENT_COMPAT)
* * Uses HTML5 entities, disallowing &#013; (ENT_HTML5)
* * Invalid characters are replaced with <EFBFBD> (ENT_SUBSTITUTE)
*
* Already existing HTML entities will be encoded as well.
*
* @param string $content The content to encode
*
* @return string The encoded content
*/
public static function escape($content)
{
return htmlspecialchars($content, ENT_COMPAT | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8');
}
/**
* sprintf()-like helper method
*
* This allows to use sprintf with ValidHtml elements, but with the
* advantage that they'll not be rendered immediately. The result is an
* instance of FormattedString, being ValidHtml
*
* Usage:
*
* echo Html::sprintf('Hello %s!', Html::tag('strong', $name));
*
* @param $string
2018-05-05 01:04:25 +02:00
* @return FormattedString
*/
2018-05-05 01:04:25 +02:00
public static function sprintf($string)
{
$args = func_get_args();
array_shift($args);
2018-05-05 01:04:25 +02:00
return new FormattedString($string, $args);
}
2018-05-05 00:18:45 +02:00
/**
* Accept any input and try to convert it to ValidHtml
*
* Returns the very same element in case it's already valid
2018-05-05 00:18:45 +02:00
*
* @param $any
* @return ValidHtml
* @throws InvalidArgumentException
2018-05-05 00:18:45 +02:00
*/
public static function wantHtml($any)
{
if ($any instanceof ValidHtml) {
return $any;
} elseif (static::canBeRenderedAsString($any)) {
return new Text($any);
} elseif (is_array($any)) {
2018-05-05 01:04:25 +02:00
$html = new HtmlDocument();
2018-05-05 00:18:45 +02:00
foreach ($any as $el) {
$html->add(static::wantHtml($el));
}
return $html;
} else {
throw new InvalidArgumentException(sprintf(
2018-05-05 00:18:45 +02:00
'String, Html Element or Array of such expected, got "%s"',
Error::getPhpTypeName($any)
));
2018-05-05 00:18:45 +02:00
}
}
/**
* Whether a given variable can be rendered as a string
*
2018-05-05 00:18:45 +02:00
* @param $any
* @return bool
2018-05-05 00:18:45 +02:00
*/
public static function canBeRenderedAsString($any)
2018-05-05 00:18:45 +02:00
{
return is_string($any) || is_int($any) || is_null($any) || is_float($any);
2018-05-05 00:18:45 +02:00
}
2017-06-23 00:20:26 +02:00
/**
* @param $name
* @param $arguments
2018-05-05 01:04:25 +02:00
* @return HtmlElement
2017-06-23 00:20:26 +02:00
*/
public static function __callStatic($name, $arguments)
{
$attributes = array_shift($arguments);
$content = array_shift($arguments);
2017-06-23 00:20:26 +02:00
return static::tag($name, $attributes, $content);
2017-06-23 00:20:26 +02:00
}
/**
* @deprecated Use {@link Html::encode()} instead
*/
public static function escapeForHtml($content)
2018-05-05 00:18:45 +02:00
{
return static::escape($content);
}
2018-05-05 00:18:45 +02:00
/**
* @deprecated Use {@link Error::render()} instead
2018-05-05 00:18:45 +02:00
*/
public static function renderError($error)
2018-05-05 00:18:45 +02:00
{
return Error::render($error);
2018-05-05 00:18:45 +02:00
}
}