generated from yii-tools/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
588de14
commit e0a6a97
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |