Skip to content

Commit

Permalink
Add HasListItemContainer::class. (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Sep 13, 2023
1 parent 51550a0 commit 8f0aefa
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/Attribute/Component/HasListItemContainer.php
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;
}
}
64 changes: 64 additions & 0 deletions tests/Attribute/Component/HasListItemContainerTest.php
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'));
}
}

0 comments on commit 8f0aefa

Please sign in to comment.