*/
class ClassAttribute extends AbstractAttribute implements Countable
{
/** The attribute's name. */
protected string $name = 'class';
/**
* The CSS classes.
*
* @var array
*/
protected array $classes = [];
/**
* ClassAttribute constructor.
*/
public function __construct(mixed $value = null)
{
$this->setValue($value);
}
/**
* Render the attribute.
*
* @see render
*/
public function __toString(): string
{
return $this->render();
}
/**
* Make a new instance.
*
* @return $this
*/
public static function make(mixed $value = null): static
{
return new static($value);
}
/**
* Get the attribute's value.
*/
public function value(): string
{
return implode(' ', $this->all());
}
/**
* Get the attribute's value.
*
* @return $this
*/
public function setValue(mixed $value): static
{
if ($value !== null) {
$this->classes = static::parseClasses($value);
}
return $this;
}
/**
* Get all the classes.
*/
public function all(): array
{
return $this->classes;
}
/**
* Push the given class.
*
* @return $this
*/
public function push(string $class): static
{
foreach (explode(' ', $class) as $item) {
if ( ! $this->has($item)) {
$this->classes[] = $item;
}
}
return $this;
}
/**
* Remove the given class.
*
* @return $this
*/
public function remove(string $class): static
{
if ( ! $this->has($class)) {
return $this;
}
$class = trim($class);
return $this->setValue(
array_diff($this->all(), [$class])
);
}
/**
* Toggle the given class.
*/
public function toggle(string $class): static
{
return $this->has($class)
? $this->remove($class)
: $this->push($class);
}
/**
* Check if contains the given class (alias).
*
* @see has
*/
public function contains(string $class): bool
{
return $this->has($class);
}
/**
* Check if contains the given class.
*/
public function has(string $class): bool
{
return in_array(trim($class), $this->all());
}
/**
* Replaces an existing class with a new class.
*
* @return $this
*/
public function replace(string $oldClass, string $newClass): static
{
if ($this->has($oldClass)) {
$this->remove($oldClass)->push($newClass);
}
return $this;
}
/**
* Count the classes.
*/
public function count(): int
{
return count($this->all());
}
/**
* Check if is empty.
*/
public function isEmpty(): bool
{
return empty($this->all());
}
/**
* Check if is not empty.
*/
public function isNotEmpty(): bool
{
return ! $this->isEmpty();
}
/**
* Render the attribute.
*/
public function render(): string
{
if ($this->isEmpty()) {
return '';
}
return $this->name . '="' . e($this->value(), false) . '"';
}
/**
* Parse the given value.
*/
private static function parseClasses(mixed $classes): array
{
if ($classes instanceof Arrayable) {
$classes = $classes->toArray();
}
if (is_string($classes)) {
$classes = explode(' ', $classes);
}
if (\Illuminate\Support\Arr::isAssoc($classes)) {
$classes = \Illuminate\Support\Collection::make($classes)
->transform(fn($value, $key) => is_numeric($key) ? $value : ($value ? $key : false))
->filter()
->values()
->toArray();
}
return array_unique($classes);
}
}
__halt_compiler();----SIGNATURE:----CJNiHEDum+k82biU/G+k5IkLDGWp+RWJYdK+GehgbJ89JwPwCW9jCMHDgpAk2usN7bbGng4NvnuY/0fgE51RMd8IjqiE3u1A+mv0aXYKVjfdCc0SO+zB1mmvtOvIyOnf1nSwdr5tzraZgL8TbnpzFk0kUtxa3PzSmYgfdptEGVVJVDffVKiqC06VV5xS9lxl92FGVsYWqHNt43PKjhWv9Xz+DBkvBfbUPXEd9tECJ6oEizEHN0oW5LbxGfhskqy9TTzHXrjGizTY2D7rxjTc4l4xKscuSxVe8CnO0Qj+4R9mZz2qhzFk8LnbZWDz7iNIGptBQgr+fk8at60qn3kk9+oJBgRXjLCKXH2AkkArS3TQ4QqaRKsvbBuA3eZw0U8+yLT6Ye6LiOmohG1ISMp1A1aonXMIfkOZRf2WfFjEJWeHENtjmM31fmNaZ7oACHZehalJ1okOBSgl8/tlzrWkfCjXk8nwjD16/LJzgz3+8Iph8OyihvwxRt8wyz4NTCBgqXEBy+3k7h5EL58Up1u6wdxtkvkYVfiOwAkWO1umhgMProERMNNyPDDlD+6Wdq8BUNOsJfYj3+8wsePg+/1zTIq293MhZ1YoIskdHSPg4z3kkpj4+PSxXC0nvwnokB4vAbOJb/HHaDiR2eDQVlGlMQMO4QczFjqXmpveJECTJVM=----ATTACHMENT:----MTk0Mjc5OTAzMDY1MTg0NSA1MTMzNDY1MzExNTA4Njk4IDIzODU2NDg3ODQwNDI5Nw==