0 follower

Final Class Yiisoft\Bootstrap5\NavLink

InheritanceYiisoft\Bootstrap5\NavLink

NavLink represents a navigation link item in a Bootstrap Nav component.

Each NavLink can be either active, disabled, or a regular link. The label can be either a string or a Stringable object, and can be optionally encoded.

Example: `php // Create a standard nav link NavLink::to('Home', '/');

// Create an active nav link NavLink::to('Current Page', '#', true);

// Create a disabled nav link NavLink::to('Disabled', '#', false, true);

// Create a tab with content NavLink::tab('Tab 1', 'Content 1');

// Create an active tab with content NavLink::tab('Tab 2', 'Content 2', true); `

Public Methods

Hide inherited methods

Method Description Defined By
active() Sets the active state of the nav item. Yiisoft\Bootstrap5\NavLink
attributes() Sets the HTML attributes for the nav item. Yiisoft\Bootstrap5\NavLink
content() Sets the content of the tab pane. Yiisoft\Bootstrap5\NavLink
disabled() Sets the disabled state of the nav item. Yiisoft\Bootstrap5\NavLink
encodeContent() Sets weather to HTML-encode the content. Yiisoft\Bootstrap5\NavLink
encodeLabel() Sets weather to HTML-encode the label. Yiisoft\Bootstrap5\NavLink
getAttributes() Yiisoft\Bootstrap5\NavLink
getContent() Yiisoft\Bootstrap5\NavLink
getId() Yiisoft\Bootstrap5\NavLink
getLabel() Yiisoft\Bootstrap5\NavLink
getPaneAttributes() Yiisoft\Bootstrap5\NavLink
getUrl() Yiisoft\Bootstrap5\NavLink
getUrlAttributes() Yiisoft\Bootstrap5\NavLink
hasContent() Yiisoft\Bootstrap5\NavLink
isActive() Yiisoft\Bootstrap5\NavLink
isDisabled() Yiisoft\Bootstrap5\NavLink
isVisible() Yiisoft\Bootstrap5\NavLink
label() Sets the label text for the nav item. Yiisoft\Bootstrap5\NavLink
paneAttributes() Sets the HTML attributes for the tab pane. Yiisoft\Bootstrap5\NavLink
paneId() Sets the ID of the nav component. Yiisoft\Bootstrap5\NavLink
shouldEncodeContent() Yiisoft\Bootstrap5\NavLink
shouldEncodeLabel() Yiisoft\Bootstrap5\NavLink
tab() Creates a {@see NavLink} instance for a tab. Yiisoft\Bootstrap5\NavLink
to() Creates a {@see NavLink} instance. Yiisoft\Bootstrap5\NavLink
url() Sets the URL for the nav item. Yiisoft\Bootstrap5\NavLink
urlAttributes() Sets HTML attributes for the nav item link. Yiisoft\Bootstrap5\NavLink
visible() Sets the visibility of the nav item. Yiisoft\Bootstrap5\NavLink

Method Details

Hide inherited methods

active() public method

Sets the active state of the nav item.

public self active ( boolean $enabled )
$enabled boolean

Whether the nav 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;
}

            
attributes() public method

Sets the HTML attributes for the nav item.

public self attributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes.

                public function attributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = $attributes;
    return $new;
}

            
content() public method

Sets the content of the tab pane.

public self content ( string|\Stringable $content )
$content string|\Stringable

The content of the tab pane.

return self

A new instance with the specified content.

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

            
disabled() public method

Sets the disabled state of the nav item.

public self disabled ( boolean $disabled )
$disabled boolean

Whether the nav item is disabled.

return self

A new instance with the specified disabled state.

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

            
encodeContent() public method

Sets weather to HTML-encode the content.

public self encodeContent ( boolean $enabled )
$enabled boolean

Whether to encode the content.

return self

New instance with the specified encoded setting.

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

            
encodeLabel() public method

Sets weather to HTML-encode the label.

public self encodeLabel ( boolean $enabled )
$enabled boolean

Whether to encode the label.

return self

New instance with the specified encoded setting.

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

            
getAttributes() public method

public array getAttributes ( )
return array

The HTML attributes for the nav item.

                public function getAttributes(): array
{
    return $this->attributes;
}

            
getContent() public method

public string|\Stringable getContent ( )
return string|\Stringable

The content of the tab pane.

                public function getContent(): string|Stringable
{
    return $this->content;
}

            
getId() public method

public string getId ( )

                public function getId(): string
{
    /** @psalm-var non-empty-string|null $id */
    return match ($this->paneId) {
        true => $this->paneAttributes['id'] ?? Html::generateId('pane'),
        '', false => throw new InvalidArgumentException('The tab pane ID must be specified.'),
        default => $this->paneId,
    };
}

            
getLabel() public method

public string|\Stringable getLabel ( )
return string|\Stringable

The label of Stringable object.

                public function getLabel(): string|Stringable
{
    return $this->label;
}

            
getPaneAttributes() public method

public array getPaneAttributes ( )
return array

The HTML attributes for the tab pane.

                public function getPaneAttributes(): array
{
    return $this->paneAttributes;
}

            
getUrl() public method

public string|null getUrl ( )
return string|null

The URL of the nav item.

                public function getUrl(): string|null
{
    return $this->url;
}

            
getUrlAttributes() public method

public array getUrlAttributes ( )
return array

The HTML attributes for the nav item link.

                public function getUrlAttributes(): array
{
    return $this->urlAttributes;
}

            
hasContent() public method

public boolean hasContent ( )
return boolean

Whether the nav item has content.

                public function hasContent(): bool
{
    return $this->content !== '';
}

            
isActive() public method

public boolean isActive ( )
return boolean

Whether the nav item is active.

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

            
isDisabled() public method

public boolean isDisabled ( )
return boolean

Whether the nav item is disabled.

                public function isDisabled(): bool
{
    return $this->disabled;
}

            
isVisible() public method

public boolean isVisible ( )
return boolean

Whether the nav item is visible.

                public function isVisible(): bool
{
    return $this->visible;
}

            
label() public method

Sets the label text for the nav item.

public self label ( string|\Stringable $label )
$label string|\Stringable

The label text or Stringable object

return self

New instance with the specified label text.

                public function label(string|Stringable $label): self
{
    $new = clone $this;
    $new->label = $label;
    return $new;
}

            
paneAttributes() public method

Sets the HTML attributes for the tab pane.

See also \Yiisoft\Html\Html::renderTagAttributes() for details on how attributes are rendered.

public self paneAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

New instance with the specified pane attributes.

                public function paneAttributes(array $attributes): self
{
    $new = clone $this;
    $new->paneAttributes = $attributes;
    return $new;
}

            
paneId() public method

Sets the ID of the nav component.

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

The ID of the alert component. If true, an ID will be generated automatically.

return self

A new instance with the specified ID.

                public function paneId(bool|string $id): self
{
    $new = clone $this;
    $new->paneId = $id;
    return $new;
}

            
shouldEncodeContent() public method

public boolean shouldEncodeContent ( )
return boolean

Whether the content should be encoded.

                public function shouldEncodeContent(): bool
{
    return $this->encodeContent;
}

            
shouldEncodeLabel() public method

public boolean shouldEncodeLabel ( )
return boolean

Whether the label should be encoded.

                public function shouldEncodeLabel(): bool
{
    return $this->encodeLabel;
}

            
tab() public static method

Creates a {@see NavLink} instance for a tab.

public static self tab ( string|\Stringable $label, string|\Stringable $content, boolean $active false, boolean $encodeLabel true, boolean $encodeContent true, array $attributes = [], array $urlAttributes = [], boolean|string $paneId true, array $paneAttributes = [], boolean $visible true )
$label string|\Stringable

The label of the tab.

$content string|\Stringable

The content of the tab pane.

$active boolean

Whether the tab is active.

$encodeLabel boolean

Whether the label should be encoded.

$encodeContent boolean

Whether the content should be encoded.

$attributes array

The HTML attributes for the nav item.

$urlAttributes array

The HTML attributes for the nav item link.

$paneId boolean|string
$paneAttributes array

The HTML attributes for the tab pane.

$visible boolean

Whether the nav item is visible.

return self

A new instance with the specified attributes.

throws InvalidArgumentException

If the tab is both active and disabled.

                public static function tab(
    string|Stringable $label,
    string|Stringable $content,
    bool $active = false,
    bool $encodeLabel = true,
    bool $encodeContent = true,
    array $attributes = [],
    array $urlAttributes = [],
    bool|string $paneId = true,
    array $paneAttributes = [],
    bool $visible = true,
): self {
    return new self(
        active: $active,
        attributes: $attributes,
        encodeLabel: $encodeLabel,
        encodeContent: $encodeContent,
        label: $label,
        url: '#',
        urlAttributes: $urlAttributes,
        visible: $visible,
        content: $content,
        paneId: $paneId,
        paneAttributes: $paneAttributes,
    );
}

            
to() public static method

Creates a {@see NavLink} instance.

public static self to ( string|\Stringable $label '', string|null $url null, boolean $active false, boolean $disabled false, boolean $encodeLabel true, array $attributes = [], array $urlAttributes = [], boolean $visible true )
$label string|\Stringable

The label of the link.

$url string|null

The URL of the link.

$active boolean

Whether the link is active.

$disabled boolean

Whether the link is disabled.

$encodeLabel boolean

Whether the label should be encoded.

$attributes array

The HTML attributes for the nav item.

$urlAttributes array

The HTML attributes for the nav item link.

$visible boolean

Whether the nav item is visible.

return self

A new instance with the specified attributes.

throws InvalidArgumentException

If the link is both active and disabled.

                public static function to(
    string|Stringable $label = '',
    string|null $url = null,
    bool $active = false,
    bool $disabled = false,
    bool $encodeLabel = true,
    array $attributes = [],
    array $urlAttributes = [],
    bool $visible = true,
): self {
    if ($active && $disabled) {
        throw new InvalidArgumentException('A nav link cannot be both active and disabled.');
    }
    return new self(
        active: $active,
        attributes: $attributes,
        encodeLabel: $encodeLabel,
        disabled: $disabled,
        label: $label,
        url: $url,
        urlAttributes: $urlAttributes,
        visible: $visible,
    );
}

            
url() public method

Sets the URL for the nav item.

public self url ( string|null $url )
$url string|null

The URL or null for no URL.

return self

New instance with the specified URL.

                public function url(string|null $url): self
{
    $new = clone $this;
    $new->url = $url;
    return $new;
}

            
urlAttributes() public method

Sets HTML attributes for the nav item link.

See also \Yiisoft\Html\Html::renderTagAttributes() for details on how attributes are rendered.

public self urlAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

New instance with the specified link attributes.

                public function urlAttributes(array $attributes): self
{
    $new = clone $this;
    $new->urlAttributes = $attributes;
    return $new;
}

            
visible() public method

Sets the visibility of the nav item.

public self visible ( boolean $enabled )
$enabled boolean

Whether the nav item is visible.

return self

A new instance with the specified visibility.

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