Skip to content

Commit

Permalink
Add containerTemplate method to HasContainer trait. (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 31, 2023
1 parent d431b8c commit 9362547
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/Attribute/Custom/HasContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
trait HasContainer
{
protected array $containerAttributes = [];
protected string $containerTemplate = '';

/**
* Enable or disable the container tag.
Expand Down Expand Up @@ -85,6 +86,26 @@ public function containerTag(string $value): static
return $new;
}

/**
* Set the container template.
*
* @param string $template The template for the container.
*
* @return static A new instance of the current class with the specified container template.
*/
public function containerTemplate(string $template): static
{
$new = clone $this;
$new->containerTemplate = $template;

return $new;
}

/**
* Get the container id.
*
* @return string The container id.
*/
public function getContainerId(): string|null
{
$id = null;
Expand Down
17 changes: 9 additions & 8 deletions tests/Attribute/Custom/HasContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public function getContainerClass(): string

$this->assertEmpty($instance->getContainerClass());

$instance = $instance->containerClass('test-class');
$instance = $instance->containerClass('class');

$this->assertSame('test-class', $instance->getContainerClass());
$this->assertSame('class', $instance->getContainerClass());

$instance = $instance->containerClass('test-class-1');
$instance = $instance->containerClass('class-1');

$this->assertSame('test-class test-class-1', $instance->getContainerClass());
$this->assertSame('class class-1', $instance->getContainerClass());

$instance = $instance->containerClass('test-override-class', true);
$instance = $instance->containerClass('override-class', true);

$this->assertSame('test-override-class', $instance->getContainerClass());
$this->assertSame('override-class', $instance->getContainerClass());
}

public function testException(): void
Expand Down Expand Up @@ -65,9 +65,9 @@ public function testContainerGetId(): void

$this->assertNull($instance->getContainerId());

$instance = $instance->containerAttributes(['id' => 'test-id']);
$instance = $instance->containerAttributes(['id' => 'id']);

$this->assertSame('test-id', $instance->getContainerId());
$this->assertSame('id', $instance->getContainerId());
}

public function testImmutability(): void
Expand All @@ -82,5 +82,6 @@ public function testImmutability(): void
$this->assertNotSame($instance, $instance->containerAttributes([]));
$this->assertNotSame($instance, $instance->containerClass(''));
$this->assertNotSame($instance, $instance->containerTag('span'));
$this->assertNotSame($instance, $instance->containerTemplate(''));
}
}

0 comments on commit 9362547

Please sign in to comment.