generated from yii-tools/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
HasListItemContainer::class
. (#131)
- Loading branch information
1 parent
51550a0
commit 8f0aefa
Showing
2 changed files
with
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPForge\Html\Attribute\Component; | ||
|
||
use InvalidArgumentException; | ||
use PHPForge\Html\Helper\CssClass; | ||
|
||
/** | ||
* Is used by widgets that implement container methods. | ||
*/ | ||
trait HasListItemContainer | ||
{ | ||
protected bool $listItemContainer = false; | ||
protected array $listItemContainerAttributes = []; | ||
|
||
/** | ||
* Enable or disable the container tag for list items. | ||
* | ||
* @param bool $value `true` to enable the container, `false` to disable. | ||
* | ||
* @return static A new instance of the current class with the specified container for list items. | ||
*/ | ||
public function listItemcontainer(bool $value): static | ||
{ | ||
$new = clone $this; | ||
$new->listItemContainer = $value; | ||
|
||
return $new; | ||
} | ||
|
||
/** | ||
* Set the `HTML` attributes for the container for list items. | ||
* | ||
* @param array $values Attribute values indexed by attribute names. | ||
* | ||
* @return static A new instance of the current class with the specified container attributes for list items. | ||
*/ | ||
public function listItemContainerAttributes(array $values = []): static | ||
{ | ||
$new = clone $this; | ||
$new->listItemcontainerAttributes = $values; | ||
|
||
return $new; | ||
} | ||
|
||
/** | ||
* Set the `CSS` class for the container for list items. | ||
* | ||
* @param string $value The CSS class name. | ||
* | ||
* @return static A new instance of the current class with the specified container class for list items. | ||
*/ | ||
public function listItemContainerClass(string $value): static | ||
{ | ||
$new = clone $this; | ||
CssClass::add($new->listItemContainerAttributes, $value); | ||
|
||
return $new; | ||
} | ||
|
||
/** | ||
* Set the container tag name for list items. | ||
* | ||
* @param string $value The tag name for the container element for list items. | ||
* | ||
* @return static A new instance of the current class with the specified container tag for list items. | ||
* | ||
* @throws InvalidArgumentException If the container tag is an empty string. | ||
*/ | ||
public function listItemContainerTag(string $value): static | ||
{ | ||
if ($value === '') { | ||
throw new InvalidArgumentException('The container tag must be a non-empty string.'); | ||
} | ||
|
||
$new = clone $this; | ||
$new->listItemcontainerTag = $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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPForge\Html\Tests\Attribute\Component; | ||
|
||
use InvalidArgumentException; | ||
use PHPForge\Html\Attribute\Component\HasListItemContainer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class HasListItemContainerTest extends TestCase | ||
{ | ||
public function testClass(): void | ||
{ | ||
$instance = new class() { | ||
use HasListItemContainer; | ||
|
||
protected string $listItemContainerTag = 'div'; | ||
|
||
public function getListItemContainerClass(): string | ||
{ | ||
return $this->listItemContainerAttributes['class'] ?? ''; | ||
} | ||
}; | ||
|
||
$this->assertEmpty($instance->getListItemContainerClass()); | ||
|
||
$instance = $instance->listItemContainerClass('foo'); | ||
|
||
$this->assertSame('foo', $instance->getListItemContainerClass()); | ||
|
||
$instance = $instance->listItemContainerClass('bar'); | ||
|
||
$this->assertSame('foo bar', $instance->getListItemContainerClass()); | ||
} | ||
|
||
public function testException(): void | ||
{ | ||
$instance = new class() { | ||
use HasListItemContainer; | ||
|
||
protected string $listItemContainerTag = ''; | ||
}; | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The container tag must be a non-empty string.'); | ||
|
||
$instance->listItemContainerTag(''); | ||
} | ||
|
||
public function testImmutablity(): void | ||
{ | ||
$instance = new class() { | ||
use HasListItemContainer; | ||
|
||
protected string $containerTag = 'div'; | ||
}; | ||
|
||
$this->assertNotSame($instance, $instance->listItemContainer(true)); | ||
$this->assertNotSame($instance, $instance->listItemContainerAttributes([])); | ||
$this->assertNotSame($instance, $instance->listItemContainerClass('')); | ||
$this->assertNotSame($instance, $instance->listItemContainerTag('span')); | ||
} | ||
} |