diff --git a/CHANGELOG.md b/CHANGELOG.md index 9297697..5ccf57b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index d9cc678..fdfefb2 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/HasTag.php b/src/HasTag.php new file mode 100644 index 0000000..7486042 --- /dev/null +++ b/src/HasTag.php @@ -0,0 +1,36 @@ +tag = $value; + + return $new; + } +} diff --git a/tests/HasTagTest.php b/tests/HasTagTest.php new file mode 100644 index 0000000..b2fde1d --- /dev/null +++ b/tests/HasTagTest.php @@ -0,0 +1,59 @@ +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()); + } +}