Skip to content

Commit 33b506a

Browse files
Add trait HasTag class. (#21)
1 parent 588de14 commit 33b506a

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Bug #18: Add `linkTag()` method in `HasLinkCollection` trait (@terabytesoftw)
2424
- Enh #19: Add trait `HasLinkContainerCollection` class (@terabytesoftw)
2525
- Enh #20: Add trait `HasLinkActiveTag` class (@terabytesoftw)
26+
- Enh #21: Add trait `HasTag` class (@terabytesoftw)
2627

2728
## 0.1.0 March 5, 2024
2829

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ List of traits avaibles to use in your classes:
5858
- [HasSeparator](src/HasSeparator.php)
5959
- [HasSuffixCollection](src/HasSuffixCollection.php)
6060
> Methods available: `suffix()`, `suffixAttributes()`, `suffixClass()`, `suffixTag()`.
61+
- [HasTag](src/HasTag.php)
6162
- [HasTagName](src/HasTagName.php)
6263
- [HasTemplate](src/HasTemplate.php)
6364
- [HasUncheckedCollection](src/HasUncheckedCollection.php)

src/HasTag.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace UIAwesome\Html\Concern;
6+
7+
/**
8+
* Is used by widgets that implement the tag method.
9+
*/
10+
trait HasTag
11+
{
12+
protected false|string $tag = false;
13+
14+
/**
15+
* Set the tag of the element.
16+
*
17+
* @param false|string $value The tag name for the element.
18+
* If `false` the tag will be disabled.
19+
*
20+
* @throws \InvalidArgumentException If the container tag is an empty string.
21+
*
22+
* @return static A new instance of the current class with the specified tag.
23+
* If `false` the tag will be disabled.
24+
*/
25+
public function tag(false|string $value): static
26+
{
27+
if ($value === '') {
28+
throw new \InvalidArgumentException('The tag cannot be an empty string.');
29+
}
30+
31+
$new = clone $this;
32+
$new->tag = $value;
33+
34+
return $new;
35+
}
36+
}

tests/HasTagTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace UIAwesome\Html\Concern\Tests;
6+
7+
use UIAwesome\Html\Concern\HasTag;
8+
9+
final class HasTagTest extends \PHPUnit\Framework\TestCase
10+
{
11+
public function testException(): void
12+
{
13+
$instance = new class () {
14+
use HasTag;
15+
};
16+
17+
$this->expectException(\InvalidArgumentException::class);
18+
$this->expectExceptionMessage('The tag cannot be an empty string.');
19+
20+
$instance->tag('');
21+
}
22+
23+
public function testImmutability(): void
24+
{
25+
$instance = new class () {
26+
use HasTag;
27+
28+
protected string $tagName = '';
29+
};
30+
31+
$this->assertNotSame($instance, $instance->tag('div'));
32+
}
33+
34+
public function testTag(): void
35+
{
36+
$instance = new class () {
37+
use HasTag;
38+
39+
public function getTag(): false|string
40+
{
41+
return $this->tag;
42+
}
43+
};
44+
45+
$this->assertFalse($instance->getTag());
46+
47+
$instance = $instance->tag('div');
48+
49+
$this->assertSame('div', $instance->getTag());
50+
51+
$instance = $instance->tag('span');
52+
53+
$this->assertSame('span', $instance->getTag());
54+
55+
$instance = $instance->tag(false);
56+
57+
$this->assertFalse($instance->getTag());
58+
}
59+
}

0 commit comments

Comments
 (0)