*
* @method \Arcanedev\Html\Elements\HtmlElement|mixed attributeIf(bool $condition, string $attribute, mixed $value = null)
* @method \Arcanedev\Html\Elements\HtmlElement|mixed attributeUnless(bool $condition, string $attribute, mixed $value = null)
* @method \Arcanedev\Html\Elements\HtmlElement|mixed attributeIfNotNull(mixed $valueToCheck, string $attribute, mixed $value = null)
*/
class HtmlElement implements HtmlElementContract
{
use Concerns\HasAttributes;
use Concerns\HasChildElements;
use Concerns\HasConditionalMethods;
use \Illuminate\Support\Traits\Macroable {
__call as __callMacro;
}
/** The tag type. */
protected string $tag;
/**
* HtmlElement constructor.
*/
public function __construct()
{
$this->initAttributes();
$this->initChildren();
}
/**
* Render the element to string (magic method).
*/
public function __toString(): string
{
return $this->toHtml();
}
/**
* @inheritDoc
*/
public function __call($name, array $arguments = [])
{
if (\Illuminate\Support\Str::endsWith($name, $this->supportedConditions)) {
foreach ($this->supportedConditions as $condition) {
if (method_exists($this, $method = str_replace($condition, '', $name))) {
return $this->callConditionalMethod($condition, $method, $arguments);
}
}
}
return $this->__callMacro($name, $arguments);
}
/**
* Clone the object.
*/
public function __clone()
{
$this->attributes = clone $this->attributes;
$this->children = clone $this->children;
}
/**
* Make a html element.
*
* @return $this
*/
public static function make(): static
{
return new static();
}
/**
* Create a element with tag.
*/
public static function withTag(string $tag): static
{
return static::make()->setTag($tag);
}
/**
* Set an id attribute.
*
* @return $this
*/
public function id(string $id): static
{
return $this->attribute('id', $id);
}
/**
* Get the class attribute.
*/
public function classList(): ClassAttribute
{
return $this->getAttributes()->classList();
}
/**
* Add a class (alias).
*
* @return $this
*/
public function class(iterable|string $class): static
{
return tap(clone $this, function (HtmlElement $elt) use ($class): void {
$elt->getAttributes()->addClass($class);
});
}
/**
* Push a class to the list.
*
* @return $this
*/
public function pushClass(string $class): static
{
return tap(clone $this, function (HtmlElement $elt) use ($class): void {
$elt->classList()->push($class);
});
}
/**
* Set the style attribute.
*
* @return $this
*/
public function style(array|string $style): static
{
if (is_array($style)) {
$style = implode('; ', array_map(
fn($value, $attribute) => "{$attribute}: {$value}",
$style,
array_keys($style)
));
}
return $this->attribute('style', $style);
}
/**
* Set the data attribute.
*
* @return $this
*/
public function data(array|string $name, mixed $value = null): static
{
return $this->attributes(
\Illuminate\Support\Collection::make(is_array($name) ? $name : [$name => $value])
->mapWithKeys(fn($mapValue, $mapKey) => ["data-{$mapKey}" => $mapValue])
);
}
/**
* Set the text.
*
* @return $this
*/
public function text(mixed $text, bool $doubleEncode = true): static
{
return $this->html(e($text, $doubleEncode));
}
/**
* Add a html child/children.
*
* @return $this
*/
public function html(mixed $html): static
{
if ($this->isVoidElement()) {
throw InvalidHtmlException::onTag($this->getTag());
}
return $this->setNewChildren($html);
}
/**
* Open the html element.
*/
public function open(): \Illuminate\Support\HtmlString
{
$attributes = $this->getAttributes();
$html = $attributes->isNotEmpty()
? "<{$this->getTag()} {$attributes->render()}>"
: "<{$this->getTag()}>";
return new \Illuminate\Support\HtmlString(
$html . $this->getChildren()->toHtml()
);
}
/**
* Close the html element.
*/
public function close(): \Illuminate\Support\HtmlString
{
return new \Illuminate\Support\HtmlString(
$this->isVoidElement() ? '' : "{$this->getTag()}>"
);
}
/**
* Render the element to HtmlString object.
*/
public function render(): \Illuminate\Support\HtmlString
{
return new \Illuminate\Support\HtmlString($this->toHtml());
}
/**
* Render the element to string.
*/
public function toHtml(): string
{
return $this->open()->toHtml() .
$this->close()->toHtml();
}
/**
* Check if the tag is a void element.
*/
public function isVoidElement(): bool
{
return in_array($this->getTag(), [
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',
'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr',
]);
}
/**
* Set the tag property.
*/
protected function setTag(string $tag): static
{
$this->tag = $tag;
return $this;
}
/**
* Get the tag type.
*
* @throws MissingTagException
*/
protected function getTag(): string
{
$this->ensureHasTag();
return $this->tag;
}
/**
* Ensure the tag property is defined.
*
* @throws MissingTagException
*/
protected function ensureHasTag(): void
{
if (empty($this->tag)) {
throw MissingTagException::onClass(static::class);
}
}
}
__halt_compiler();----SIGNATURE:----LZEo1vQQu84BT4woI/k/G4KkuLH76OOkQohkzJC065g1AO7TSHYVg/VoC6aZakktdgIbf+92BJ5tkSEFEmDkRbliiyB+MSrbbPu3iL/VaFza3J6C19Cm5qgDoZ9uPV7hNClnfgyZBjx6OAbcKY52polWSEfWKacY8+jevgKTtutM9W8hG67wuPRgIApQSx9YrYnf6RtgNf2GT5phA0fESw47Q0Y2LIBRWwTvfLoiygGJb9nTlI/izWPSnb3m9Z19XfGBuUEqkS2Usz8A0nMl80VN7lgn5D140M6gld6+xV0s8DhefuoCrEsEqPzqP0HYEwMgBcto6teCriZL7mJ+O1dBaa2EDTrnrpyJOK9Jwn/rgYsBZn6TFPSqijxe38TrpeAgFVXxf6q13B3fbbAC8Wtjh8g+1cEl1ItO1QTIctLbPuuFq5PqElX9WLL9ZH+H1ALycuduj9XYG4zJv+6vtFqO+9yuP6tvYoPAgaKW2pcNoPqpSelJOlQU4m5pSpBXSWYrPt0w2Za1yt2FWrCqsfDQIBYcvs61spe3wiWw9ySX+Mjl/vCNbyYqDObetzvcEwyJ7ZtrseVktlxm5rLK+1QBi94F2zM8TPI+0q151FsTh83S0D+ukewHABiwFYhI8Jxo1ySe3t5QAj5FC9xbtDdyy9Ic6X3u8lT6RKl7Sj8=----ATTACHMENT:----NTY1NjA3NzM4ODUwMjI3MCA2NzMzMjQ0NTM1NzMyODQxIDc2ODgzMTA2MDMzOTM2MDU=