Skip to content

Commit

Permalink
Add methods containerPrefix() and containerSuffix. (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Aug 28, 2023
1 parent 6ab9505 commit 2b60049
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
34 changes: 34 additions & 0 deletions src/Attribute/Custom/HasContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

use InvalidArgumentException;
use PHPForge\Html\Helper\CssClass;
use PHPForge\Html\Helper\Encode;
use PHPForge\Widget\ElementInterface;

/**
* Is used by widgets that implement container methods.
*/
trait HasContainer
{
protected array $containerAttributes = [];
protected string $containerPrefix = '';
protected string $containerSuffix = '';

/**
* Enable or disable the container tag.
Expand Down Expand Up @@ -59,6 +63,36 @@ public function containerClass(string $value): static
return $new;
}

/**
* Set the `HTML` container prefix content.
*
* @param string|ElementInterface ...$values The `HTML` container prefix content.
*
* @return static A new instance of the current class with the specified container prefix content.
*/
public function containerPrefix(string|ElementInterface ...$values): static
{
$new = clone $this;
$new->containerPrefix = Encode::create()->santizeXSS(...$values);

return $new;
}

/**
* Set the `HTML` container suffix content.
*
* @param string|ElementInterface ...$values The `HTML` container suffix content.
*
* @return static A new instance of the current class with the specified container suffix content.
*/
public function containerSuffix(string|ElementInterface ...$values): static
{
$new = clone $this;
$new->containerSuffix = Encode::create()->santizeXSS(...$values);

return $new;
}

/**
* Set the container tag name.
*
Expand Down
26 changes: 14 additions & 12 deletions src/Base/AbstractButtonToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,14 @@ abstract class AbstractButtonToggle extends Element
protected array $attributes = [];
private string $type = 'menu';

public function type(string $type): static
public function alert(): static
{
$allowedTypes = ['alert', 'menu', 'sidebar'];

if (in_array($type, $allowedTypes, true) === false) {
throw new InvalidArgumentException(
sprintf('The type "%s" is not allowed for the "ButtonToggle::class".', $type)
);
}

$new = clone $this;
$new->type = $type;
return $this->type('alert');
}

return $new;
public function sidebar(): static
{
return $this->type('sidebar');
}

/**
Expand Down Expand Up @@ -155,4 +149,12 @@ private function renderSidebarToggle(array $attributes, string $id): string
)
->render();
}

private function type(string $type): static
{
$new = clone $this;
$new->type = $type;

return $new;
}
}
2 changes: 2 additions & 0 deletions tests/Attribute/Custom/HasContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function testImmutablity(): void
$this->assertNotSame($instance, $instance->container(true));
$this->assertNotSame($instance, $instance->containerAttributes([]));
$this->assertNotSame($instance, $instance->containerClass(''));
$this->assertNotSame($instance, $instance->containerPrefix(''));
$this->assertNotSame($instance, $instance->containerSuffix(''));
$this->assertNotSame($instance, $instance->containerTag('span'));
}
}
8 changes: 0 additions & 8 deletions tests/ButtonToggle/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@
*/
final class ExceptionTest extends TestCase
{
public function testType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The type "foo" is not allowed for the "ButtonToggle::class".');

ButtonToggle::widget()->type('foo')->render();
}

public function testWithoutID(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
3 changes: 2 additions & 1 deletion tests/ButtonToggle/ImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function testImmutable(): void
{
$buttonToggle = ButtonToggle::widget();

$this->assertNotSame($buttonToggle, $buttonToggle->type('menu'));
$this->assertNotSame($buttonToggle, $buttonToggle->alert());
$this->assertNotSame($buttonToggle, $buttonToggle->sidebar());
}
}
8 changes: 4 additions & 4 deletions tests/ButtonToggle/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testToggleAlert(): void
<svg xmlns="http://www.w3.org/2000/svg" class="w-3 h-3" aria-hidden="true" fill="none" viewBox="0 0 14 14"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/></svg>
</button>
HTML,
ButtonToggle::widget()->id('id')->type('alert')->render(),
ButtonToggle::widget()->alert()->id('id')->render(),
);
}

Expand All @@ -86,7 +86,7 @@ public function testToggleAlertContent(): void
test-content
</button>
HTML,
ButtonToggle::widget()->content('test-content')->id('id')->type('alert')->render(),
ButtonToggle::widget()->alert()->content('test-content')->id('id')->render(),
);
}

Expand Down Expand Up @@ -128,7 +128,7 @@ public function testToggleSidebar(): void
<svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20"><path clip-rule="evenodd" fill-rule="evenodd" d="M2 4.75A.75.75 0 012.75 4h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 4.75zm0 10.5a.75.75 0 01.75-.75h7.5a.75.75 0 010 1.5h-7.5a.75.75 0 01-.75-.75zM2 10a.75.75 0 01.75-.75h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 10z"/></svg>
</button>
HTML,
ButtonToggle::widget()->id('id')->type('sidebar')->render(),
ButtonToggle::widget()->id('id')->sidebar()->render(),
);
}

Expand All @@ -140,7 +140,7 @@ public function testToggleSidebarContent(): void
test-content
</button>
HTML,
ButtonToggle::widget()->content('test-content')->id('id')->type('sidebar')->render(),
ButtonToggle::widget()->content('test-content')->id('id')->sidebar()->render(),
);
}
}

0 comments on commit 2b60049

Please sign in to comment.