format = Html::wantHtml($format);
if ($args !== null) {
if (! is_iterable($args)) {
throw new InvalidArgumentException(sprintf(
'%s expects parameter two to be iterable, got %s instead',
__METHOD__,
get_php_type($args)
));
}
foreach ($args as $key => $val) {
if (! is_scalar($val) || (is_string($val) && ! is_numeric($val))) {
$val = Html::wantHtml($val);
}
$this->args[$key] = $val;
}
}
}
/**
* Create a new {@link sprintf()}-like formatted HTML string
*
* @param string $format
* @param mixed ...$args
*
* @return static
*/
public static function create($format, ...$args)
{
return new static($format, $args);
}
/**
* Render text to HTML when treated like a string
*
* Calls {@link render()} internally in order to render the text to HTML.
* Exceptions will be automatically caught and returned as HTML string as well using {@link Error::render()}.
*
* @return string
*/
public function __toString()
{
try {
return $this->render();
} catch (Exception $e) {
return Error::render($e);
}
}
public function render()
{
return vsprintf(
$this->format->render(),
$this->args
);
}
}