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

#156 - render identifier instead of @id for typed identification #157

Merged
merged 1 commit into from
Jan 2, 2021
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ $localBusiness = Schema::localBusiness()

### Identifier

As of v2.6.0 the `identifier` key is replaced by `@id`. This is due to the definition for the `ld+json` syntax.
As of v2.6.0 the `identifier` key is replaced by `@id` for simple string identifiers. This is due to the definition for the `ld+json` syntax.

> All schema.org syntaxes already have built-in representation for URIs and URLs, e.g. in Microdata 'itemid', in RDFa 1.1, 'resource', **in JSON-LD, '@id'.**
>
> — [schema.org/docs](https://schema.org/docs/datamodel.html#identifierBg) // [PR#102](https://github.com/spatie/schema-org/pull/102)
> — [schema.org/docs](https://schema.org/docs/datamodel.html#identifierBg) // [PR#102](https://github.com/spatie/schema-org/pull/102) // [PR#157](https://github.com/spatie/schema-org/pull/157)

### Advanced Usage

Expand Down
2 changes: 1 addition & 1 deletion generator/templates/static/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function serializeProperty($property)

protected function serializeIdentifier()
{
if (isset($this['identifier'])) {
if (isset($this['identifier']) && !$this['identifier'] instanceof Type ) {
$this->setProperty('@id', $this['identifier']);
unset($this['identifier']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function serializeProperty($property)

protected function serializeIdentifier()
{
if (isset($this['identifier'])) {
if (isset($this['identifier']) && !$this['identifier'] instanceof Type ) {
$this->setProperty('@id', $this['identifier']);
unset($this['identifier']);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/BaseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PHPUnit\Framework\TestCase;
use Spatie\SchemaOrg\BaseType;
use Spatie\SchemaOrg\Exceptions\InvalidProperty;
use Spatie\SchemaOrg\Product;
use Spatie\SchemaOrg\PropertyValue;

class BaseTypeTest extends TestCase
{
Expand Down Expand Up @@ -325,6 +327,26 @@ public function it_replaces_identifier_with_at_id_property()
$this->assertEquals($expected, $type->toArray());
}

/** @test */
public function it_can_render_identifier_for_typed_identifiers()
{
$productType = new Product();
$propertyValue = new PropertyValue();
$propertyValue->identifier('#1');
$productType->setProperty('identifier', $propertyValue);

$expected = [
'@context' => 'https://schema.org',
'@type' => 'Product',
'identifier' => [
"@type" => "PropertyValue",
"@id" => "#1",
],
];

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

/** @test */
public function it_can_reference_type_by_identifier()
{
Expand Down