-
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.
- Loading branch information
Showing
3 changed files
with
100 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
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,62 @@ | ||
<?php declare (strict_types = 1); | ||
|
||
namespace Wavevision\Utils\DOM; | ||
|
||
use Nette\SmartObject; | ||
|
||
final class DataAttribute | ||
{ | ||
|
||
use SmartObject; | ||
|
||
private string $currentName; | ||
|
||
private string $currentValue; | ||
|
||
public function __construct(string $name, ?string $prefix = null) | ||
{ | ||
$this->currentName = $prefix ? "data-$prefix-$name" : "data-$name"; | ||
$this->currentValue = ''; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->asString(); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @return array<string, string> | ||
*/ | ||
public function asArray($value = null): array | ||
{ | ||
$this->value($value); | ||
return [$this->currentName => $this->currentValue]; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function asString($value = null): string | ||
{ | ||
$this->value($value); | ||
return sprintf('%s="%s"', $this->currentName, $this->currentValue); | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->currentName; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function value($value = null): string | ||
{ | ||
if ($value !== null) { | ||
$this->currentValue = (string)$value; | ||
} | ||
return $this->currentValue; | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php declare (strict_types = 1); | ||
|
||
namespace Wavevision\UtilsTests\DOM; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Wavevision\Utils\DOM\DataAttribute; | ||
|
||
class DataAttributeTest extends TestCase | ||
{ | ||
|
||
public function testAsArray(): void | ||
{ | ||
$this->assertEquals(['data-test' => ''], $this->createDataAttribute()->asArray()); | ||
} | ||
|
||
public function testAsString(): void | ||
{ | ||
$this->assertEquals('data-test=""', $this->createDataAttribute()->asString()); | ||
$this->assertEquals('data-prefix-test=""', (string)$this->createDataAttribute('prefix')); | ||
} | ||
|
||
public function testName(): void | ||
{ | ||
$this->assertEquals('data-prefix-test', $this->createDataAttribute('prefix')->name()); | ||
} | ||
|
||
public function testValue(): void | ||
{ | ||
$this->assertEquals('something', $this->createDataAttribute()->value('something')); | ||
} | ||
|
||
private function createDataAttribute(?string $prefix = null): DataAttribute | ||
{ | ||
return new DataAttribute('test', $prefix); | ||
} | ||
|
||
} |