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 HasItemActiveContainer trait for item active container functional…
…ity. (#180)
- Loading branch information
1 parent
9f2571b
commit de1c219
Showing
2 changed files
with
129 additions
and
0 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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPForge\Html\Attribute\Tag; | ||
|
||
use InvalidArgumentException; | ||
use PHPForge\Html\Helper\CssClass; | ||
|
||
/** | ||
* Is used by widgets that implement the item active container methods. | ||
*/ | ||
trait HasItemActiveContainer | ||
{ | ||
protected array $itemActiveContainerAttributes = []; | ||
|
||
public function itemActiveContainer(bool $value): static | ||
{ | ||
$new = clone $this; | ||
$new->itemActiveContainer = $value; | ||
|
||
return $new; | ||
} | ||
|
||
public function itemActiveContainerAttributes(array $value): static | ||
{ | ||
$new = clone $this; | ||
$new->itemActiveContainerAttributes = $value; | ||
|
||
return $new; | ||
} | ||
|
||
public function itemActiveContainerClass(string $value, bool $override = false): static | ||
{ | ||
$new = clone $this; | ||
CssClass::add($new->itemActiveContainerAttributes, $value, $override); | ||
|
||
return $new; | ||
} | ||
|
||
public function itemActiveContainerTag(string $value): static | ||
{ | ||
if ($value === '') { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'The item container tag must be a non-empty string. "%s" given.', | ||
$value, | ||
), | ||
); | ||
} | ||
|
||
$new = clone $this; | ||
$new->itemActiveContainerTag = $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPForge\Html\Tests\Attribute\Tag; | ||
|
||
use InvalidArgumentException; | ||
use PHPForge\Html\Attribute\Tag\HasItemActiveContainer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class HasItemActiveContainerTest extends TestCase | ||
{ | ||
public function testClass(): void | ||
{ | ||
$instance = new class () { | ||
use HasItemActiveContainer; | ||
|
||
protected bool $itemActiveContainer = false; | ||
protected array $itemActiveContainerAttributes = []; | ||
protected string $itemActiveContainerTag = 'div'; | ||
|
||
public function getItemActiveContainerClass(): string | ||
{ | ||
return $this->itemActiveContainerAttributes['class'] ?? ''; | ||
} | ||
}; | ||
|
||
$this->assertEmpty($instance->getItemActiveContainerClass()); | ||
|
||
$instance = $instance->itemActiveContainerClass('class'); | ||
|
||
$this->assertSame('class', $instance->getItemActiveContainerClass()); | ||
|
||
$instance = $instance->itemActiveContainerClass('class-1'); | ||
|
||
$this->assertSame('class class-1', $instance->getItemActiveContainerClass()); | ||
|
||
$instance = $instance->itemActiveContainerClass('override-class', true); | ||
|
||
$this->assertSame('override-class', $instance->getItemActiveContainerClass()); | ||
} | ||
|
||
public function testException(): void | ||
{ | ||
$instance = new class () { | ||
use HasItemActiveContainer; | ||
|
||
protected string $itemActiveContainerTag = ''; | ||
}; | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The item container tag must be a non-empty string.'); | ||
|
||
$instance->itemActiveContainerTag(''); | ||
} | ||
|
||
public function testImmutability(): void | ||
{ | ||
$instance = new class () { | ||
use HasItemActiveContainer; | ||
|
||
protected bool $itemActiveContainer = false; | ||
protected array $itemActiveContainerAttributes = []; | ||
protected string $itemActiveContainerTag = 'div'; | ||
}; | ||
|
||
$this->assertNotSame($instance, $instance->itemActiveContainer(false)); | ||
$this->assertNotSame($instance, $instance->itemActiveContainerAttributes([])); | ||
$this->assertNotSame($instance, $instance->itemActiveContainerClass('')); | ||
$this->assertNotSame($instance, $instance->itemActiveContainerTag('span')); | ||
} | ||
} |