$value) {
if (is_string($wrapper)) {
$result->addHtml(Html::tag($wrapper, $value));
} elseif (is_callable($wrapper)) {
$result->add($wrapper($name, $value));
} else {
throw new InvalidArgumentException(sprintf(
'Wrapper must be callable or a string in Html::wrapEach(), got "%s"',
get_php_type($wrapper)
));
}
}
return $result;
}
/**
* Ensure that the given content of mixed type is converted to an instance of {@link ValidHtml}
*
* Returns the very same element in case it's already an instance of {@link ValidHtml}.
*
* @param mixed $any
*
* @return ValidHtml
*
* @throws InvalidArgumentException In case the given content is of an unsupported type
*/
public static function wantHtml($any)
{
if ($any instanceof ValidHtml) {
return $any;
} elseif (static::canBeRenderedAsString($any)) {
return new Text($any);
} elseif (is_iterable($any)) {
$html = new HtmlDocument();
foreach ($any as $el) {
if ($el !== null) {
$html->addHtml(static::wantHtml($el));
}
}
return $html;
} else {
throw new InvalidArgumentException(sprintf(
'String, Html Element or Array of such expected, got "%s"',
get_php_type($any)
));
}
}
/**
* Accept any input and return it as list of ValidHtml
*
* @param mixed $content
*
* @return ValidHtml[]
*/
public static function wantHtmlList($content)
{
$list = [];
if ($content === null) {
return $list;
} elseif (! is_iterable($content)) {
$list[] = static::wantHtml($content);
} elseif ($content instanceof ValidHtml) {
$list[] = $content;
} else {
foreach ($content as $part) {
$list = array_merge($list, static::wantHtmlList($part));
}
}
return $list;
}
/**
* Get whether the given variable be rendered as a string
*
* @param mixed $any
*
* @return bool
*/
public static function canBeRenderedAsString($any)
{
return is_scalar($any) || is_null($any) || (
is_object($any) && method_exists($any, '__toString')
);
}
/**
* Forward inaccessible static method calls to {@link Html::tag()} with the method's name as tag
*
* @param string $name
* @param array $arguments
*
* @return HtmlElement
*/
public static function __callStatic($name, $arguments)
{
$attributes = array_shift($arguments);
$content = array_shift($arguments);
return static::tag($name, $attributes, $content);
}
/**
* @deprecated Use {@link Html::encode()} instead
*/
public static function escapeForHtml($content)
{
return static::escape($content);
}
/**
* @deprecated Use {@link Error::render()} instead
*/
public static function renderError($error)
{
return Error::render($error);
}
}