';
return $this->toHtmlString($html);
}
/**
* Create a listing HTML element.
*
* @param string $type
* @param array $list
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString|string
*/
protected function listing($type, $list, $attributes = [])
{
$html = '';
if (count($list) === 0) {
return $html;
}
// Essentially we will just spin through the list and build the list of the HTML
// elements from the array. We will also handled nested lists in case that is
// present in the array. Then we will build out the final listing elements.
foreach ($list as $key => $value) {
$html .= $this->listingElement($key, $type, $value);
}
$attributes = $this->attributes($attributes);
return $this->toHtmlString("<{$type}{$attributes}>{$html}{$type}>");
}
/**
* Create the HTML for a listing element.
*
* @param mixed $key
* @param string $type
* @param mixed $value
*
* @return string
*/
protected function listingElement($key, $type, $value)
{
if (is_array($value)) {
return $this->nestedListing($key, $type, $value);
} else {
return '
' . e($value, false) . '
';
}
}
/**
* Create the HTML for a nested listing attribute.
*
* @param mixed $key
* @param string $type
* @param mixed $value
*
* @return string
*/
protected function nestedListing($key, $type, $value)
{
if (is_int($key)) {
return $this->listing($type, $value);
} else {
return '
' . $key . $this->listing($type, $value) . '
';
}
}
/**
* Build an HTML attribute string from an array.
*
* @param array $attributes
*
* @return string
*/
public function attributes($attributes)
{
$html = [];
foreach ((array) $attributes as $key => $value) {
$element = $this->attributeElement($key, $value);
if (! is_null($element)) {
$html[] = $element;
}
}
return count($html) > 0 ? ' ' . implode(' ', $html) : '';
}
/**
* Build a single attribute element.
*
* @param string $key
* @param string $value
*
* @return string
*/
protected function attributeElement($key, $value)
{
// For numeric keys we will assume that the value is a boolean attribute
// where the presence of the attribute represents a true value and the
// absence represents a false value.
// This will convert HTML attributes such as "required" to a correct
// form instead of using incorrect numerics.
if (is_numeric($key)) {
return $value;
}
// Treat boolean attributes as HTML properties
if (is_bool($value) && $key !== 'value') {
return $value ? $key : '';
}
if (is_array($value) && $key === 'class') {
return 'class="' . implode(' ', $value) . '"';
}
if (! is_null($value)) {
return $key . '="' . e($value, false) . '"';
}
}
/**
* Obfuscate a string to prevent spam-bots from sniffing it.
*
* @param string $value
*
* @return string
*/
public function obfuscate($value)
{
$safe = '';
foreach (str_split($value) as $letter) {
if (ord($letter) > 128) {
return $letter;
}
// To properly obfuscate the value, we will randomly convert each letter to
// its entity or hexadecimal representation, keeping a bot from sniffing
// the randomly obfuscated letters out of the string on the responses.
switch (rand(1, 3)) {
case 1:
$safe .= '' . ord($letter) . ';';
break;
case 2:
$safe .= '' . dechex(ord($letter)) . ';';
break;
case 3:
$safe .= $letter;
}
}
return $safe;
}
/**
* Generate a meta tag.
*
* @param string $name
* @param string $content
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function meta($name, $content, array $attributes = [])
{
$defaults = compact('name', 'content');
$attributes = array_merge($defaults, $attributes);
return $this->toHtmlString('attributes($attributes) . '>');
}
/**
* Generate an html tag.
*
* @param string $tag
* @param mixed $content
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function tag($tag, $content, array $attributes = [])
{
$content = is_array($content) ? implode('', $content) : $content;
return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . $this->toHtmlString($content) . '' . $tag . '>');
}
/**
* Transform the string to an Html serializable object
*
* @param $html
*
* @return \Illuminate\Support\HtmlString
*/
protected function toHtmlString($html)
{
return new HtmlString($html);
}
/**
* Dynamically handle calls to the class.
*
* @param string $method
* @param array $parameters
*
* @return \Illuminate\Contracts\View\View|mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
if (static::hasComponent($method)) {
return $this->componentCall($method, $parameters);
}
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
throw new BadMethodCallException("Method {$method} does not exist.");
}
}