0 follower

Final Class Yiisoft\Bootstrap5\AccordionItem

InheritanceYiisoft\Bootstrap5\AccordionItem

AccordionItem represents a single collapsible item within an Accordion widget.

Each item consists of a header that can be clicked to show/hide the body content. The item can be set as active (expanded) by default and supports both raw and HTML content.

For example:

AccordionItem::to(
    'Collapsible Group Item #1'
    'Any sufficiently advanced technology is indistinguishable from magic.',
);

Constants

Hide inherited constants

Constant Value Description Defined By
DEFAULT_ID_PREFIX 'collapse' Yiisoft\Bootstrap5\AccordionItem

Method Details

Hide inherited methods

active() public method

Sets the active state.

public self active ( boolean $enabled )
$enabled boolean

Whether the accordion item is active.

return self

A new instance with the specified active state.

                public function active(bool $enabled): self
{
    $new = clone $this;
    $new->active = $enabled;
    return $new;
}

            
body() public method

Sets the body content.

public self body ( string $content )
$content string

The body content.

return self

A new instance with the specified body content.

                public function body(string $content): self
{
    $new = clone $this;
    $new->body = $content;
    return $new;
}

            
encodeBody() public method

Sets whether to encode the body content.

public self encodeBody ( boolean $enabled )
$enabled boolean

Whether to encode the body content.

return self

A new instance with the specified encoding setting.

                public function encodeBody(bool $enabled): self
{
    $new = clone $this;
    $new->encodeBody = $enabled;
    return $new;
}

            
encodeHeader() public method

Sets whether to encode the header content.

public self encodeHeader ( boolean $enabled )
$enabled boolean

Whether to encode the header content.

return self

A new instance with the specified encoding setting.

                public function encodeHeader(bool $enabled): self
{
    $new = clone $this;
    $new->encodeHeader = $enabled;
    return $new;
}

            
getBody() public method

public string getBody ( )
return string

The encoded body content. If {@see \Yiisoft\Bootstrap5\encodeBody} is false, the body content will not be encoded.

                public function getBody(): string
{
    return $this->encodeBody ? Html::encode($this->body) : $this->body;
}

            
getHeader() public method

public string getHeader ( )
return string

The encoded header content. If {@see \Yiisoft\Bootstrap5\encodeHeader} is false, the header content will not be encoded.

                public function getHeader(): string
{
    return $this->encodeHeader ? Html::encode($this->header) : $this->header;
}

            
getId() public method

Returns the ID.

public boolean|string getId ( )
return boolean|string

The ID.

throws InvalidArgumentException

If the "id" property is invalid.

                public function getId(): bool|string
{
    return match ($this->id) {
        true => Html::generateId(self::DEFAULT_ID_PREFIX . '-'),
        '', false => throw new InvalidArgumentException('The "id" property must be a non-empty string or `true`.'),
        default => $this->id,
    };
}

            
header() public method

Sets the header content.

public self header ( string $content )
$content string

The header content.

return self

A new instance with the specified header content.

                public function header(string $content): self
{
    $new = clone $this;
    $new->header = $content;
    return $new;
}

            
id() public method

Sets the ID.

public self id ( boolean|string $id )
$id boolean|string

The ID of the accordion item. If true, an auto-generated ID will be used. If false, no ID will be set.

return self

A new instance with the specified ID.

throws InvalidArgumentException

If the "id" property is empty or false.

                public function id(bool|string $id): self
{
    if ($id === '' || $id === false) {
        throw new InvalidArgumentException('The "id" property must be a non-empty string or `true`.');
    }
    $new = clone $this;
    $new->id = $id;
    return $new;
}

            
isActive() public method

public boolean isActive ( )
return boolean

Whether the item is active.

                public function isActive(): bool
{
    return $this->active;
}

            
to() public static method

Creates a new {@see AccordionItem} instance.

public static self to ( string $header '', string $body '', boolean|string $id true, boolean $encodeHeader true, boolean $encodeBody true, boolean $active false )
$header string

The header content.

$body string

The body content.

$id boolean|string

The ID of the accordion item. If true, an auto-generated ID will be used. If false, no ID will be set.

$encodeHeader boolean

Whether to encode the header content.

$encodeBody boolean

Whether to encode the body content.

$active boolean

Whether the item is active.

return self

A new instance with the specified configuration.

                public static function to(
    string $header = '',
    string $body = '',
    bool|string $id = true,
    bool $encodeHeader = true,
    bool $encodeBody = true,
    bool $active = false
): self {
    return new self($active, $body, $encodeBody, $encodeHeader, $header, $id);
}