Skip to content

Commit

Permalink
Add HasToggleButton::class trait. (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 29, 2023
1 parent 728d15f commit 2368ecf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Attribute/Component/HasToggleButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Attribute\Component;

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

/**
* Is used by widgets that implement the toggle button method.
*/
trait HasToggleButton
{
protected string $toggleButton = '';

/**
* Set the button toogle for the toggle.
*
* @param ElementInterface|string ...$values The button content.
*
* @return static A new instance of the current class with the specified toggle button.
*/
public function toggleButton(string|ElementInterface ...$values): static
{
$new = clone $this;
$new->toggleButton = Encode::santizeXSS(...$values);

return $new;
}
}
20 changes: 20 additions & 0 deletions tests/Attribute/Component/HasToggleButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Attribute\Component;

use PHPForge\Html\Attribute\Component\HasToggleButton;
use PHPUnit\Framework\TestCase;

final class HasToggleButtonTest extends TestCase
{
public function testImmutability(): void
{
$instance = new class () {
use HasToggleButton;
};

$this->assertNotSame($instance, $instance->toggleButton(''));
}
}

0 comments on commit 2368ecf

Please sign in to comment.