Skip to content

Commit

Permalink
Add custom attributes HasRenderStringable::class, `HasToggle::class…
Browse files Browse the repository at this point in the history
…`. (#51)
  • Loading branch information
terabytesoftw committed Jul 20, 2023
1 parent f953320 commit bb208c6
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 13 deletions.
23 changes: 23 additions & 0 deletions src/Attribute/Custom/HasRenderStringable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Attribute\Custom;

use Stringable;

/**
* Allows rendering an object of type Stringable.
*/
trait HasRenderStringable
{
/**
* Returns a new instance with the stringable value.
*
* @param string|Stringable $value The stringable value.
*/
public function renderStringable(string|Stringable $value): string
{
return $value instanceof Stringable ? $value->__toString() : $value;
}
}
108 changes: 108 additions & 0 deletions src/Attribute/Custom/HasToggle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Attribute\Custom;

use PHPForge\Html\Helper\CssClass;

/**
* Is used by components that can have a toggle.
*/
trait HasToggle
{
private bool $toggle = true;
private array $toggleAttributes = [];
private string $toggleClass = '';
private string $toggleContent = '';
private string $toggleId = '';

/**
* Returns a new instance, specifying disabled toggle.
*/
public function notToggle(): static
{
$new = clone $this;
$new->toggle = false;

return $new;
}

/**
* Returns a new instance specifying the `HTML` attributes for the toggle button.
*
* @param array $value The `HTML` attributes for the toggle button.
*/
public function toggleAttributes(array $value): static
{
$new = clone $this;
$new->toggleAttributes = array_merge($value, $new->toggleAttributes);

return $new;
}

/**
* Returns a new instance specifying the `CSS` class for the toggle button.
*
* @param string $value The `CSS` class for the toggle button.
*/
public function toggleClass(string $value): static
{
$new = clone $this;
CssClass::add($new->toggleAttributes, $value);

return $new;
}

/**
* Returns a new instance specifying the toggle button content.
*
* @param string $value The toggle button content.
*/
public function toggleContent(string $value): static
{
$new = clone $this;
$new->toggleContent = $value;

return $new;
}

/**
* Returns a new instance specifying the toggle data attribute.
*
* @param string $name The toggle data attribute name.
*/
public function toggleDataAttribute(string $name, string $value): static
{
$new = clone $this;
$new->toggleAttributes["data-$name"] = $value;

return $new;
}

/**
* Returns a new instance specifying the toggle button ID.
*
* @param string $value The toggle button ID.
*/
public function toggleId(string $value): static
{
$new = clone $this;
$new->toggleId = $value;

return $new;
}

/**
* Returns a new instance specifying the toggle on click event.
*
* @param string $value The toggle on click event.
*/
public function toggleOnClick(string $value): static
{
$new = clone $this;
$new->toggleAttributes['onclick'] = $value;

return $new;
}
}
3 changes: 0 additions & 3 deletions tests/Attribute/Custom/HasContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public function testContainerClass(): void
$instance = new class () {
use HasContainer;

protected array $attributes = [];
protected bool $container = true;
protected string $containerTag = 'div';

Expand All @@ -41,7 +40,6 @@ public function testException(): void
$instance = new class () {
use HasContainer;

protected array $attributes = [];
protected bool $container = true;
protected string $containerTag = '';
};
Expand All @@ -57,7 +55,6 @@ public function testImmutablity(): void
$instance = new class () {
use HasContainer;

protected array $attributes = [];
protected string $containerTag = 'div';
};

Expand Down
4 changes: 0 additions & 4 deletions tests/Attribute/Custom/HasContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public function testStringable(): void
$instance = new class () {
use HasContent;

protected array $attributes = [];

public function getContent(): string
{
return $this->content;
Expand All @@ -69,8 +67,6 @@ public function testStringableWithEncodeFalse(): void
$instance = new class () {
use HasContent;

protected array $attributes = [];

public function getContent(): string
{
return $this->content;
Expand Down
6 changes: 0 additions & 6 deletions tests/Attribute/Custom/HasIconTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public function testIconClass(): void
$instance = new class () {
use HasIcon;

protected array $attributes = [];

public function getIconAttributes(): array
{
return $this->iconAttributes;
Expand All @@ -38,8 +36,6 @@ public function testIconContainerClass(): void
$instance = new class () {
use HasIcon;

protected array $attributes = [];

public function getIconContainerAttributes(): array
{
return $this->iconContainerAttributes;
Expand All @@ -61,8 +57,6 @@ public function testImmutablity(): void
{
$instance = new class () {
use HasIcon;

protected array $attributes = [];
};

$this->assertNotSame($instance, $instance->iconAttributes([]));
Expand Down
32 changes: 32 additions & 0 deletions tests/Attribute/Custom/HasRenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Attribute\Custom;

use PHPForge\Html\Attribute\Custom\HasRenderStringable;
use PHPUnit\Framework\TestCase;
use Stringable;

final class HasRenderTest extends TestCase
{
public function testRender(): void
{
$instance = new class () {
use HasRenderStringable;
};

$this->assertSame('test', $instance->renderStringable('test'));
$this->assertSame(
'test',
$instance->renderStringable(
new class () implements Stringable {
public function __toString(): string
{
return 'test';
}
}
)
);
}
}
85 changes: 85 additions & 0 deletions tests/Attribute/Custom/HasToggleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Attribute\Custom;

use PHPForge\Html\Attribute\Custom\HasToggle;
use PHPUnit\Framework\TestCase;

final class HasToggleTest extends TestCase
{
public function testImmutablity(): void
{
$instance = new class () {
use HasToggle;
};

$this->assertNotSame($instance, $instance->notToggle());
$this->assertNotSame($instance, $instance->toggleAttributes([]));
$this->assertNotSame($instance, $instance->toggleClass(''));
$this->assertNotSame($instance, $instance->toggleContent(''));
$this->assertNotSame($instance, $instance->toggleDataAttribute('id', ''));
$this->assertNotSame($instance, $instance->toggleId(''));
$this->assertNotSame($instance, $instance->toggleOnClick(''));
}

public function testNotToggle(): void
{
$instance = new class () {
use HasToggle;

public function getToggle(): bool
{
return $this->toggle;
}
};

$this->assertTrue($instance->getToggle());
$this->assertFalse($instance->notToggle()->getToggle());
}

public function testToggleAttributes(): void
{
$instance = new class () {
use HasToggle;

public function getToggleAttributes(): array
{
return $this->toggleAttributes;
}
};

$this->assertSame([], $instance->getToggleAttributes());

$instance = $instance->toggleAttributes(['class' => 'test']);

$this->assertSame(['class' => 'test'], $instance->getToggleAttributes());

$instance = $instance->toggleAttributes(['disabled' => 'true']);

$this->assertSame(['disabled' => 'true', 'class' => 'test'], $instance->getToggleAttributes());
}

public function testToggleClass(): void
{
$instance = new class () {
use HasToggle;

public function getToggleAttributes(): array
{
return $this->toggleAttributes;
}
};

$this->assertSame([], $instance->getToggleAttributes());

$instance = $instance->toggleClass('test');

$this->assertSame(['class' => 'test'], $instance->getToggleAttributes());

$instance = $instance->toggleClass('test1');

$this->assertSame(['class' => 'test test1'], $instance->getToggleAttributes());
}
}

0 comments on commit bb208c6

Please sign in to comment.