-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Setono/add-consentable-tag
Add consentable script tag
- Loading branch information
Showing
4 changed files
with
130 additions
and
3 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
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,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); | ||
} | ||
} |
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,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()); | ||
} | ||
} |