Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Bug #18: Add `linkTag()` method in `HasLinkCollection` trait (@terabytesoftw)
- Enh #19: Add trait `HasLinkContainerCollection` class (@terabytesoftw)
- Enh #20: Add trait `HasLinkActiveTag` class (@terabytesoftw)
- Enh #21: Add trait `HasTag` class (@terabytesoftw)

## 0.1.0 March 5, 2024

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ List of traits avaibles to use in your classes:
- [HasSeparator](src/HasSeparator.php)
- [HasSuffixCollection](src/HasSuffixCollection.php)
> Methods available: `suffix()`, `suffixAttributes()`, `suffixClass()`, `suffixTag()`.
- [HasTag](src/HasTag.php)
- [HasTagName](src/HasTagName.php)
- [HasTemplate](src/HasTemplate.php)
- [HasUncheckedCollection](src/HasUncheckedCollection.php)
Expand Down
36 changes: 36 additions & 0 deletions src/HasTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace UIAwesome\Html\Concern;

/**
* Is used by widgets that implement the tag method.
*/
trait HasTag
{
protected false|string $tag = false;

/**
* Set the tag of the element.
*
* @param false|string $value The tag name for the element.
* If `false` the tag will be disabled.
*
* @throws \InvalidArgumentException If the container tag is an empty string.
*
* @return static A new instance of the current class with the specified tag.
* If `false` the tag will be disabled.
*/
public function tag(false|string $value): static
{
if ($value === '') {
throw new \InvalidArgumentException('The tag cannot be an empty string.');
}

$new = clone $this;
$new->tag = $value;

return $new;
}
}
59 changes: 59 additions & 0 deletions tests/HasTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace UIAwesome\Html\Concern\Tests;

use UIAwesome\Html\Concern\HasTag;

final class HasTagTest extends \PHPUnit\Framework\TestCase
{
public function testException(): void
{
$instance = new class () {
use HasTag;
};

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The tag cannot be an empty string.');

$instance->tag('');
}

public function testImmutability(): void
{
$instance = new class () {
use HasTag;

protected string $tagName = '';
};

$this->assertNotSame($instance, $instance->tag('div'));
}

public function testTag(): void
{
$instance = new class () {
use HasTag;

public function getTag(): false|string
{
return $this->tag;
}
};

$this->assertFalse($instance->getTag());

$instance = $instance->tag('div');

$this->assertSame('div', $instance->getTag());

$instance = $instance->tag('span');

$this->assertSame('span', $instance->getTag());

$instance = $instance->tag(false);

$this->assertFalse($instance->getTag());
}
}