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

Reference types using @id instead of duplicating type #155

Merged
merged 5 commits into from
Nov 5, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions generator/templates/static/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public function getProperties(): array
return $this->properties;
}

/**
* @return ReferencedType|static
*/
public function referenced()
{
return new ReferencedType($this);
}

public function offsetExists($offset)
{
return array_key_exists($offset, $this->properties);
Expand Down
38 changes: 38 additions & 0 deletions generator/templates/static/ReferencedType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Spatie\SchemaOrg;

use JsonSerializable;

class ReferencedType implements Type, JsonSerializable
{
/** @var Type */
protected $type;

public function __construct(Type $type)
{
$this->type = $type;
}

public function toArray(): array
{
return [
'@id' => $this->type->toArray()['@id'] ?? null,
];
}

public function toScript(): string
{
return $this->type->toScript();
}

public function jsonSerialize()
{
return $this->toArray();
}

public function __toString(): string
{
return $this->toScript();
}
}
8 changes: 8 additions & 0 deletions src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public function getProperties(): array
return $this->properties;
}

/**
* @return ReferencedType|static
*/
public function referenced()
{
return new ReferencedType($this);
}

public function offsetExists($offset)
{
return array_key_exists($offset, $this->properties);
Expand Down
38 changes: 38 additions & 0 deletions src/ReferencedType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Spatie\SchemaOrg;

use JsonSerializable;

class ReferencedType implements Type, JsonSerializable
{
/** @var Type */
protected $type;

public function __construct(Type $type)
{
$this->type = $type;
}

public function toArray(): array
{
return [
'@id' => $this->type->toArray()['@id'] ?? null,
];
}

public function toScript(): string
{
return $this->type->toScript();
}

public function jsonSerialize()
{
return $this->toArray();
}

public function __toString(): string
{
return $this->toScript();
}
}
23 changes: 23 additions & 0 deletions tests/BaseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,29 @@ public function it_replaces_identifier_with_at_id_property()

$this->assertEquals($expected, $type->toArray());
}

/** @test */
public function it_can_reference_type_by_identifier()
{
$type1 = new AnotherDummyType();
$type1->setProperty('identifier', '#1');
$type1->setProperty('name', 'another-object');
$type2 = new DummyType();
$type2->setProperty('identifier', '#2');
$type1->setProperty('name', 'object');
$type2->setProperty('referenced', $type1->referenced());

$expected = [
'@context' => 'https://schema.org',
'@type' => 'DummyType',
'@id' => '#2',
'referenced' => [
'@id' => '#1',
],
];

$this->assertEquals($expected, $type2->toArray());
}
}

class DummyType extends BaseType
Expand Down
20 changes: 20 additions & 0 deletions tests/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,24 @@ public function it_can_do_things_conditionally()
$this->assertEquals('spatie', $graph->organization()['name']);
$this->assertNotEquals('organization', $graph->organization()['name']);
}

/** @test */
public function it_can_use_references()
{
$graph = new Graph();

$graph->blogPosting()
->identifier('https://example.com/blog/my-post/#blogPosting')
->author($graph->organization()->name('organization')->referenced())
->publisher($graph->organization()->url('https://example.com')->referenced());

$graph->organization()
->identifier('https://example.com/#organization')
->email('[email protected]');

$this->assertEquals(
'<script type="application/ld+json">{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","author":{"@id":"https:\/\/example.com\/#organization"},"publisher":{"@id":"https:\/\/example.com\/#organization"},"@id":"https:\/\/example.com\/blog\/my-post\/#blogPosting"},{"@type":"Organization","name":"organization","url":"https:\/\/example.com","email":"[email protected]","@id":"https:\/\/example.com\/#organization"}]}</script>',
$graph->toScript()
);
}
}