Skip to content

Commit

Permalink
Merge pull request #191 from smortexa/feat/nonce
Browse files Browse the repository at this point in the history
Add support for nonce attribute
  • Loading branch information
Gummibeer authored Feb 14, 2023
2 parents 4942ed1 + f287a3a commit ada9f69
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 6 deletions.
23 changes: 22 additions & 1 deletion generator/templates/static/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ abstract class BaseType implements Type, ArrayAccess, JsonSerializable
/** @var array */
protected $properties = [];

/** @var string */
protected $nonce = '';

public function getContext(): string
{
return 'https://schema.org';
Expand Down Expand Up @@ -51,6 +54,13 @@ public function if($condition, $callback)
return $this;
}

public function setNonce(string $nonce)
{
$this->nonce = $nonce;

return $this;
}

public function getProperty(string $property, $default = null)
{
return $this->properties[$property] ?? $default;
Expand Down Expand Up @@ -137,9 +147,20 @@ protected function serializeIdentifier()
}
}

public function nonceAttr(): string
{
if ($this->nonce) {
$attr = ' nonce="'.$this->nonce.'"';
} else {
$attr = '';
}

return $attr;
}

public function toScript(): string
{
return '<script type="application/ld+json">'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>';
}

public function jsonSerialize(): mixed
Expand Down
23 changes: 22 additions & 1 deletion generator/templates/twig/Graph.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Graph implements Type, ArrayAccess, JsonSerializable
/** @var string|null */
protected $context;
/** @var string */
protected $nonce = '';
public function __construct(string|array|null $context = null)
{
$this->context = $context;
Expand Down Expand Up @@ -221,6 +224,13 @@ class Graph implements Type, ArrayAccess, JsonSerializable
return $this;
}
public function setNonce(string $nonce)
{
$this->nonce = $nonce;
return $this;
}
public function toArray(): array
{
$nodes = $this->getNodes();
Expand Down Expand Up @@ -275,9 +285,20 @@ class Graph implements Type, ArrayAccess, JsonSerializable
return $this->context ?? 'https://schema.org';
}
public function nonceAttr(): string
{
if ($this->nonce) {
$attr = ' nonce="'.$this->nonce.'"';
} else {
$attr = '';
}
return $attr;
}
public function toScript(): string
{
return '<script type="application/ld+json">'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
}
public function jsonSerialize(): mixed
Expand Down
23 changes: 22 additions & 1 deletion generator/templates/twig/MultiTypedEntity.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class MultiTypedEntity implements Type, JsonSerializable
/** @var Type[] */
protected $nodes = [];
/** @var string */
protected $nonce = '';
/**
* This overloads all \Spatie\SchemaOrg\Schema construction methods.
* You can call them the same like on the \Spatie\SchemaOrg\Schema class.
Expand Down Expand Up @@ -94,6 +97,13 @@ class MultiTypedEntity implements Type, JsonSerializable
return $this;
}
public function setNonce(string $nonce)
{
$this->nonce = $nonce;
return $this;
}
public function get(string $type): Type
{
if (! $this->has($type)) {
Expand Down Expand Up @@ -162,9 +172,20 @@ class MultiTypedEntity implements Type, JsonSerializable
return 'https://schema.org';
}
public function nonceAttr(): string
{
if ($this->nonce) {
$attr = ' nonce="'.$this->nonce.'"';
} else {
$attr = '';
}
return $attr;
}
public function toScript(): string
{
return '<script type="application/ld+json">'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
}
public function jsonSerialize(): mixed
Expand Down
23 changes: 22 additions & 1 deletion src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ abstract class BaseType implements Type, ArrayAccess, JsonSerializable
/** @var array */
protected $properties = [];

/** @var string */
protected $nonce = '';

public function getContext(): string
{
return 'https://schema.org';
Expand Down Expand Up @@ -51,6 +54,13 @@ public function if($condition, $callback)
return $this;
}

public function setNonce(string $nonce)
{
$this->nonce = $nonce;

return $this;
}

public function getProperty(string $property, $default = null)
{
return $this->properties[$property] ?? $default;
Expand Down Expand Up @@ -137,9 +147,20 @@ protected function serializeIdentifier()
}
}

public function nonceAttr(): string
{
if ($this->nonce) {
$attr = ' nonce="'.$this->nonce.'"';
} else {
$attr = '';
}

return $attr;
}

public function toScript(): string
{
return '<script type="application/ld+json">'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>';
}

public function jsonSerialize(): mixed
Expand Down
23 changes: 22 additions & 1 deletion src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,9 @@ class Graph implements Type, ArrayAccess, JsonSerializable
/** @var string|null */
protected $context;

/** @var string */
protected $nonce = '';

public function __construct(string|array|null $context = null)
{
$this->context = $context;
Expand Down Expand Up @@ -1103,6 +1106,13 @@ public function show(string $type, ?string $identifier = self::IDENTIFIER_DEFAUL
return $this;
}

public function setNonce(string $nonce)
{
$this->nonce = $nonce;

return $this;
}

public function toArray(): array
{
$nodes = $this->getNodes();
Expand Down Expand Up @@ -1157,9 +1167,20 @@ public function getContext(): string|array
return $this->context ?? 'https://schema.org';
}

public function nonceAttr(): string
{
if ($this->nonce) {
$attr = ' nonce="'.$this->nonce.'"';
} else {
$attr = '';
}

return $attr;
}

public function toScript(): string
{
return '<script type="application/ld+json">'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
}

public function jsonSerialize(): mixed
Expand Down
23 changes: 22 additions & 1 deletion src/MultiTypedEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,9 @@ class MultiTypedEntity implements Type, JsonSerializable
/** @var Type[] */
protected $nodes = [];

/** @var string */
protected $nonce = '';

/**
* This overloads all \Spatie\SchemaOrg\Schema construction methods.
* You can call them the same like on the \Spatie\SchemaOrg\Schema class.
Expand Down Expand Up @@ -976,6 +979,13 @@ public function set(Type $schema)
return $this;
}

public function setNonce(string $nonce)
{
$this->nonce = $nonce;

return $this;
}

public function get(string $type): Type
{
if (! $this->has($type)) {
Expand Down Expand Up @@ -1044,9 +1054,20 @@ public function getContext(): string
return 'https://schema.org';
}

public function nonceAttr(): string
{
if ($this->nonce) {
$attr = ' nonce="'.$this->nonce.'"';
} else {
$attr = '';
}

return $attr;
}

public function toScript(): string
{
return '<script type="application/ld+json">'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
return '<script type="application/ld+json"'.$this->nonceAttr().'>'.json_encode($this, JSON_UNESCAPED_UNICODE).'</script>';
}

public function jsonSerialize(): mixed
Expand Down
14 changes: 14 additions & 0 deletions tests/BaseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@
expect($type->toScript())->toBe($expected);
});

it('can create an ld json script tag with nonce attribute', function () {
$type = new DummyType();

$type->setProperty('foo', 'bar');

$type->setNonce('baz');

$expected = '<script type="application/ld+json" nonce="baz">'.
'{"@context":"https:\/\/schema.org","@type":"DummyType","foo":"bar"}'.
'</script>';

expect($type->toScript())->toBe($expected);
});

it('can set a property via a magic call method', function () {
$type = new DummyType();

Expand Down

0 comments on commit ada9f69

Please sign in to comment.