Skip to content

Commit

Permalink
Merge pull request #17 from Setono/add-consentable-tag
Browse files Browse the repository at this point in the history
Add consentable script tag
  • Loading branch information
loevgaard authored Feb 14, 2023
2 parents b1f9f0c + 81611f0 commit 36c120f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
},
"require-dev": {
"infection/infection": "^0.26",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.17",
"setono/code-quality-pack": "^2.2"
"phpunit/phpunit": "^9.6",
"psalm/plugin-phpunit": "^0.18",
"setono/code-quality-pack": "^2.4"
},
"prefer-stable": true,
"autoload": {
Expand Down
16 changes: 16 additions & 0 deletions src/Tag/AttributesAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@

namespace Setono\TagBag\Tag;

use InvalidArgumentException;

trait AttributesAwareTrait
{
/** @var array<string, string|null> */
protected array $attributes = [];

public function hasAttribute(string $attribute): bool
{
return array_key_exists($attribute, $this->attributes);
}

public function getAttribute(string $attribute): ?string
{
if (!$this->hasAttribute($attribute)) {
throw new InvalidArgumentException(sprintf('The attribute "%s" does not exist', $attribute));
}

return $this->attributes[$attribute];
}

/**
* @return array<string, string|null>
*/
Expand Down
60 changes: 60 additions & 0 deletions src/Tag/ConsentableScriptTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Setono\TagBag\Tag;

/**
* The script tag represents script tags in the form of <script type="text/plain" src="..."></script>
*/
final class ConsentableScriptTag extends ElementTag
{
/**
* @return static
*/
public static function create(string $src, string $consentType = null): self
{
$obj = parent::createWithoutContent('script')
->withSrc($src)
->withType('text/plain')
;

if (null !== $consentType) {
$obj = $obj->withAttribute('data-consent', $consentType);
}

return $obj;
}

/**
* Returns the src attribute value for the <script src="..."> tag
*/
public function getSrc(): ?string
{
return $this->attributes['src'] ?? null;
}

/**
* @return static
*/
public function withSrc(string $src): self
{
return $this->withAttribute('src', $src);
}

/**
* Returns the type attribute for the <script type="..."> tag
*/
public function getType(): ?string
{
return $this->attributes['type'] ?? null;
}

/**
* @return static
*/
public function withType(string $type): self
{
return $this->withAttribute('type', $type);
}
}
51 changes: 51 additions & 0 deletions tests/Tag/ConsentableScriptTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Setono\TagBag\Tag;

use PHPUnit\Framework\TestCase;

final class ConsentableScriptTagTest extends TestCase
{
/**
* @test
*/
public function it_creates(): void
{
$tag = ConsentableScriptTag::create('https://example.com/script.js');

self::assertInstanceOf(TagInterface::class, $tag);
self::assertInstanceOf(ContentAwareInterface::class, $tag);
self::assertInstanceOf(AttributesAwareInterface::class, $tag);
}

/**
* @test
*/
public function it_has_default_values(): void
{
$tag = ConsentableScriptTag::create('https://example.com/script.js', 'marketing');

self::assertSame('https://example.com/script.js', $tag->getSrc());
self::assertSame('text/plain', $tag->getType());
self::assertSame('marketing', $tag->getAttribute('data-consent'));
}

/**
* @test
*/
public function it_has_immutable_setters(): void
{
$tag = ConsentableScriptTag::create('https://example.com/script.js')->withType('type');
$newTag = $tag->withSrc('https://example.com/script2.js')->withType('new_type');

self::assertNotSame($tag, $newTag);

self::assertSame('type', $tag->getType());
self::assertSame('new_type', $newTag->getType());

self::assertSame('https://example.com/script.js', $tag->getSrc());
self::assertSame('https://example.com/script2.js', $newTag->getSrc());
}
}

0 comments on commit 36c120f

Please sign in to comment.