*
* @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:----SX3ecwuNE8NnZIZgPuBEX/rQuF1aJ6iWYQCMGFJTZWSSJz4389QRV4TCIf73/NU/Miegi114nRHfHzQo8XcsNF7O+Wz/VOQXGxFqHJ4+A08mHm93QAyK8DcFEFCUzEqUtd6nW2/JmyzcEydsKxXZuCQ65Ded+CWQi91/JcZcIF/sWpOqKXovWDcCsOrvbXqfDp7xQg99T6Pu68SZKT+GfD2iMqg3rxRuCgMej/SrWnYVSeb2Tsd+jkBBSck2JhhEZqEOU1Sly1L5ANtOlLf+JRIY8DSB0bT/4PpgJP9QgFNDAmkx+HV9R8ePGM/Yt6c0iUY88KXz3gufOA0rGihOvw1Oqw23mbC275gSp4Q9V5zfB258p7LyED1aYvJ+DLfKRzmTgaCKi/1q7PQgnxVVv+2+IhBNY3iXhVcVoklTLH6viYXryDD4XWRC+/fZpaUjcpmpV263pJIfERwQqbN7phgOa+Ij5VE/lYkSgBIWJKQ0RNZuhKzv9++I0B4LL9qSIkPP3b2qklYyv6oSyJSxJ5qY1kHKQEiDGy/yczul2+u7k6R+xU77Vg0ZgZbH/RPNWK0BkH4b3vonEPWqRtKibxBip9BPAHfpzxEBqtGg3LvBQXSH7DGvy9mqSCB0S6C+5C77qHUYjSNreT/Q5R+aDj4EUuJ0CIcBHuAwh0wVpyk=----ATTACHMENT:----NzQ5MjkzOTEyMjA3MjE3MSA3MjM3MzY1NzgzMzYzMDE3IDE1NDQzODI3NTA4MTAxMDA=