Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Add DataAttribute utils and allow JSON serialization on it
Browse files Browse the repository at this point in the history
  • Loading branch information
rozsival committed Mar 13, 2020
1 parent 7caa617 commit 40d2f1f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
32 changes: 29 additions & 3 deletions src/Utils/DOM/DataAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Wavevision\Utils\DOM;

use JsonSerializable;
use Nette\SmartObject;
use Nette\Utils\Html;
use Wavevision\Utils\Strings;

final class DataAttribute
final class DataAttribute implements JsonSerializable
{

use SmartObject;
Expand All @@ -26,6 +27,14 @@ public function __toString(): string
return $this->asString();
}

/**
* @return array<string, string>
*/
public function jsonSerialize(): array
{
return $this->asArray();
}

/**
* @param mixed $value
* @return array<string, string>
Expand All @@ -37,11 +46,12 @@ public function asArray($value = null): array

/**
* @param Html<mixed> $element
* @param mixed $value
* @return Html<mixed>
*/
public function assign(Html $element): Html
public function assign(Html $element, $value = null): Html
{
$element->setAttribute($this->currentName, $this->currentValue);
$element->setAttribute($this->currentName, $this->value($value));
return $element;
}

Expand All @@ -53,6 +63,22 @@ public function asString($value = null): string
return sprintf('%s="%s"', $this->currentName, $this->value($value));
}

/**
* @param Html<mixed> $element
*/
public function get(Html $element): ?string
{
return $element->getAttribute($this->currentName);
}

/**
* @param Html<mixed> $element
*/
public function has(Html $element): bool
{
return $this->get($element) !== null;
}

public function name(): string
{
return $this->currentName;
Expand Down
25 changes: 20 additions & 5 deletions tests/UtilsTests/DOM/DataAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
namespace Wavevision\UtilsTests\DOM;

use Nette\Utils\Html;
use Nette\Utils\Json;
use PHPUnit\Framework\TestCase;
use Wavevision\Utils\DOM\DataAttribute;

class DataAttributeTest extends TestCase
{

public function testJsonSerialize(): void
{
$this->assertEquals('{"data-test":""}', Json::encode($this->createDataAttribute()));
}

public function testAsArray(): void
{
$this->assertEquals(['data-test' => ''], $this->createDataAttribute()->asArray());
Expand All @@ -22,9 +28,18 @@ public function testAsString(): void

public function testAssign(): void
{
$element = Html::el();
$this->createDataAttribute()->assign($element);
$this->assertSame('', $element->getAttribute('data-test'));
$attribute = $this->createDataAttribute();
$this->assertSame('', $attribute->get($attribute->assign(Html::el())));
}

public function testGet(): void
{
$this->assertNull($this->createDataAttribute()->get(Html::el()));
}

public function testHas(): void
{
$this->assertFalse($this->createDataAttribute()->has(Html::el()));
}

public function testName(): void
Expand All @@ -36,8 +51,8 @@ public function testRemove(): void
{
$attribute = $this->createDataAttribute();
$element = Html::el();
$this->assertTrue($attribute->assign($element)->getAttribute($attribute->name()) === '');
$this->assertNull($attribute->remove($element)->getAttribute($attribute->name()));
$this->assertEquals('', $attribute->get($attribute->assign($element)));
$this->assertNull($attribute->get($attribute->remove($element)));
}

public function testValue(): void
Expand Down

0 comments on commit 40d2f1f

Please sign in to comment.