Skip to content

Commit

Permalink
Merge pull request #189 from spatie/issue-185
Browse files Browse the repository at this point in the history
add support for complex context
  • Loading branch information
Gummibeer authored Nov 14, 2022
2 parents e47266a + 60fe4a6 commit 54399c0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 3 additions & 3 deletions generator/templates/twig/Graph.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Graph implements Type, ArrayAccess, JsonSerializable
/** @var string|null */
protected $context;
public function __construct(?string $context = null)
public function __construct(string|array|null $context = null)
{
$this->context = $context;
}
Expand Down Expand Up @@ -176,7 +176,7 @@ class Graph implements Type, ArrayAccess, JsonSerializable
{
// show all
if ($identifier === null) {
$this->hidden[$type] = false;
unset($this->hidden[$type]);
return $this;
}
Expand Down Expand Up @@ -270,7 +270,7 @@ class Graph implements Type, ArrayAccess, JsonSerializable
return $this->nodes;
}
public function getContext(): string
public function getContext(): string|array
{
return $this->context ?? 'https://schema.org';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ class Graph implements Type, ArrayAccess, JsonSerializable
/** @var string|null */
protected $context;

public function __construct(?string $context = null)
public function __construct(string|array|null $context = null)
{
$this->context = $context;
}
Expand Down Expand Up @@ -1058,7 +1058,7 @@ public function show(string $type, ?string $identifier = self::IDENTIFIER_DEFAUL
{
// show all
if ($identifier === null) {
$this->hidden[$type] = false;
unset($this->hidden[$type]);

return $this;
}
Expand Down Expand Up @@ -1152,7 +1152,7 @@ public function getNodes(): array
return $this->nodes;
}

public function getContext(): string
public function getContext(): string|array
{
return $this->context ?? 'https://schema.org';
}
Expand Down
21 changes: 21 additions & 0 deletions tests/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,26 @@

it('can be initialized with different context', function () {
$graph = new Graph('https://foobar.com');

expect($graph->getContext())->toBe('https://foobar.com');

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":"https:\/\/foobar.com","@graph":[]}</script>'
);
});

it('can be initialized with complex context', function () {
$graph = new Graph([
'@vocab' => 'https://schema.org/',
'@base' => 'https://domain.com/',
]);

expect($graph->getContext())->toBe([
'@vocab' => 'https://schema.org/',
'@base' => 'https://domain.com/',
]);

expect($graph->toScript())->toBe(
'<script type="application/ld+json">{"@context":{"@vocab":"https:\/\/schema.org\/","@base":"https:\/\/domain.com\/"},"@graph":[]}</script>'
);
});

0 comments on commit 54399c0

Please sign in to comment.