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 custom attributes
HasRenderStringable::class
, `HasToggle::class…
…`. (#51)
- Loading branch information
1 parent
f953320
commit bb208c6
Showing
7 changed files
with
248 additions
and
13 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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
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
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
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,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'; | ||
} | ||
} | ||
) | ||
); | ||
} | ||
} |
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,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()); | ||
} | ||
} |