Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for complex context #189

Merged
merged 1 commit into from Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions generator/templates/twig/Graph.php.twig
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
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
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>'
);
});