Skip to content

Commit

Permalink
Add DOM utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rozsival committed Jan 15, 2020
1 parent dc2fa6b commit 33e8fb0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ composer require wavevision/utils
The package contains useful classes for:

- [Arrays](./src/Utils/Arrays.php) – array helpers (manipulate, sort, extract etc.)
- [DOM](./src/Utils/DOM) – create and format data attributes for HTML elements
- [ExternalProgram](./src/Utils/ExternalProgram/Executor.php) – simple external command runner
- [Finder](./src/Utils/Finder.php) – adds sorting to [nette/finder](https://github.com/nette/finder)
- [ImageInfo](./src/Utils/ImageInfo.php) – get image content type and size
Expand Down
62 changes: 62 additions & 0 deletions src/Utils/DOM/DataAttribute.php
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;
}

}
37 changes: 37 additions & 0 deletions tests/UtilsTests/DOM/DataAttributeTest.php
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);
}

}

0 comments on commit 33e8fb0

Please sign in to comment.