2017-06-22 02:03:54 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
2017-10-09 15:23:27 +02:00
|
|
|
|
namespace dipl\Html;
|
2017-06-22 02:03:54 +02:00
|
|
|
|
|
2018-05-25 19:09:42 +02:00
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Html
|
|
|
|
|
*
|
|
|
|
|
* This is your main utility class when working with ipl\Html
|
|
|
|
|
*
|
|
|
|
|
* @package ipl\Html
|
|
|
|
|
*/
|
|
|
|
|
abstract class Html
|
2017-06-22 02:03:54 +02:00
|
|
|
|
{
|
2018-05-25 19:09:42 +02:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
2018-05-25 19:09:42 +02:00
|
|
|
|
return new HtmlElement($name, $attributes, $content);
|
|
|
|
|
}
|
2018-05-05 00:18:45 +02:00
|
|
|
|
|
2017-07-24 08:40:39 +02:00
|
|
|
|
/**
|
2018-05-25 19:09:42 +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 
 (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
|
2017-06-22 02:03:54 +02:00
|
|
|
|
*/
|
2018-05-25 19:09:42 +02:00
|
|
|
|
public static function escape($content)
|
2017-06-22 02:03:54 +02:00
|
|
|
|
{
|
2018-05-25 19:09:42 +02:00
|
|
|
|
return htmlspecialchars($content, ENT_COMPAT | ENT_HTML5 | ENT_SUBSTITUTE, 'UTF-8');
|
2017-06-22 02:03:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-05-25 19:09:42 +02:00
|
|
|
|
* 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));
|
|
|
|
|
*
|
2017-06-22 02:03:54 +02:00
|
|
|
|
* @param $string
|
2018-05-05 01:04:25 +02:00
|
|
|
|
* @return FormattedString
|
2017-06-22 02:03:54 +02:00
|
|
|
|
*/
|
2018-05-05 01:04:25 +02:00
|
|
|
|
public static function sprintf($string)
|
2017-06-22 02:03:54 +02:00
|
|
|
|
{
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift($args);
|
|
|
|
|
|
2018-05-05 01:04:25 +02:00
|
|
|
|
return new FormattedString($string, $args);
|
2017-06-22 02:03:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 00:18:45 +02:00
|
|
|
|
/**
|
2018-05-25 19:09:42 +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
|
2018-05-25 19:09:42 +02:00
|
|
|
|
* @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 {
|
2018-05-25 19:09:42 +02:00
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
2018-05-05 00:18:45 +02:00
|
|
|
|
'String, Html Element or Array of such expected, got "%s"',
|
2018-05-25 19:09:42 +02:00
|
|
|
|
Error::getPhpTypeName($any)
|
|
|
|
|
));
|
2018-05-05 00:18:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-05-25 19:09:42 +02:00
|
|
|
|
* Whether a given variable can be rendered as a string
|
|
|
|
|
*
|
2018-05-05 00:18:45 +02:00
|
|
|
|
* @param $any
|
2018-05-25 19:09:42 +02:00
|
|
|
|
* @return bool
|
2018-05-05 00:18:45 +02:00
|
|
|
|
*/
|
2018-05-25 19:09:42 +02:00
|
|
|
|
public static function canBeRenderedAsString($any)
|
2018-05-05 00:18:45 +02:00
|
|
|
|
{
|
2018-05-25 19:09:42 +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);
|
2018-05-25 19:09:42 +02:00
|
|
|
|
$content = array_shift($arguments);
|
2017-06-23 00:20:26 +02:00
|
|
|
|
|
2018-05-25 19:09:42 +02:00
|
|
|
|
return static::tag($name, $attributes, $content);
|
2017-06-23 00:20:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 02:03:54 +02:00
|
|
|
|
/**
|
2018-05-25 19:09:42 +02:00
|
|
|
|
* @deprecated Use {@link Html::encode()} instead
|
2017-06-22 02:03:54 +02:00
|
|
|
|
*/
|
2018-05-25 19:09:42 +02:00
|
|
|
|
public static function escapeForHtml($content)
|
2018-05-05 00:18:45 +02:00
|
|
|
|
{
|
2018-05-25 19:09:42 +02:00
|
|
|
|
return static::escape($content);
|
2017-06-22 02:03:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 00:18:45 +02:00
|
|
|
|
/**
|
2018-05-25 19:09:42 +02:00
|
|
|
|
* @deprecated Use {@link Error::render()} instead
|
2018-05-05 00:18:45 +02:00
|
|
|
|
*/
|
2018-05-25 19:09:42 +02:00
|
|
|
|
public static function renderError($error)
|
2018-05-05 00:18:45 +02:00
|
|
|
|
{
|
2018-05-25 19:09:42 +02:00
|
|
|
|
return Error::render($error);
|
2018-05-05 00:18:45 +02:00
|
|
|
|
}
|
2017-06-22 02:03:54 +02:00
|
|
|
|
}
|