Skip to content

Commit

Permalink
Refactor BreadcrumbLink to improve constructor property handling an…
Browse files Browse the repository at this point in the history
…d add immutable methods.
  • Loading branch information
terabytesoftw committed Jan 21, 2025
1 parent 3dc4609 commit 218da18
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
89 changes: 83 additions & 6 deletions src/BreadcrumbLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ final class BreadcrumbLink
* Use {@see BreadcrumbLink::to()} to create an instance.
*/
private function __construct(
private readonly string $label = '',
private readonly string|null $url = null,
private readonly bool $active = false,
private readonly bool $encodeLabel = true,
private readonly array $attributes = [],
private string $label = '',
private string|null $url = null,
private bool $active = false,
private bool $encodeLabel = true,
private array $attributes = [],
) {
}

Expand All @@ -51,14 +51,91 @@ private function __construct(
*/
public static function to(
string $label,
string $url = null,
string|null $url = null,
bool $active = false,
array $attributes = [],
bool $encodeLabel = true
): self {
return new self($label, $url, $active, $encodeLabel, $attributes);
}

/**
* Sets the active state of the breadcrumb link.
*
* @param bool $value Whether the breadcrumb link is active.
*
* @return self A new instance with the specified active state.
*/
public function active(bool $value): self
{
$new = clone $this;
$new->active = $value;

return $new;
}

/**
* Sets the HTML attributes for the breadcrumb link.
*
* @param array $values Attribute values indexed by attribute names.
*
* @return self A new instance with the specified attributes.
*
* @see {\Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function attributes(array $values): self
{
$new = clone $this;
$new->attributes = $values;

return $new;
}

/**
* Sets whether to HTML encode the label.
*
* @param bool $value Whether to HTML encode the label.
*
* @return self A new instance with the specified encoding behavior.
*/
public function encodeLabel(bool $value): self
{
$new = clone $this;
$new->encodeLabel = $value;

return $new;
}

/**
* Sets the label text to display.
*
* @param string $value The label text to display.
*
* @return self A new instance with the specified label.
*/
public function label(string $value): self
{
$new = clone $this;
$new->label = $value;

return $new;
}

/**
* Sets the URL for the breadcrumb link.
*
* @param string|null $value The URL for the link.
*
* @return self A new instance with the specified URL.
*/
public function url(string|null $value): self
{
$new = clone $this;
$new->url = $value;

return $new;
}

/**
* @return array The HTML attributes for the link.
*/
Expand Down
14 changes: 12 additions & 2 deletions tests/BreadcrumbLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
namespace Yiisoft\Yii\Bootstrap5\Tests;

use Yiisoft\Yii\Bootstrap5\BreadcrumbLink;
use Yiisoft\Yii\Bootstrap5\Link;

/**
* Tests for `Link`.
*
* @group breadcrumblink
* @group breadcrumb
*/
final class BreadcrumbLinkTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -48,4 +47,15 @@ public function testIsActive(): void

$this->assertTrue($active->isActive());
}

public function testImmutability(): void
{
$link = BreadcrumbLink::to('');

$this->assertNotSame($link, $link->active(false));
$this->assertNotSame($link, $link->attributes([]));
$this->assertNotSame($link, $link->encodeLabel(false));
$this->assertNotSame($link, $link->label(''));
$this->assertNotSame($link, $link->url(''));
}
}

0 comments on commit 218da18

Please sign in to comment.