diff --git a/README.md b/README.md index 5de4379a9..1e37fa5a5 100644 --- a/README.md +++ b/README.md @@ -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#156](https://github.com/spatie/schema-org/pull/157) ### Advanced Usage diff --git a/generator/templates/static/BaseType.php b/generator/templates/static/BaseType.php index 6174e7618..79832c099 100644 --- a/generator/templates/static/BaseType.php +++ b/generator/templates/static/BaseType.php @@ -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']); } diff --git a/src/BaseType.php b/src/BaseType.php index 6174e7618..79832c099 100644 --- a/src/BaseType.php +++ b/src/BaseType.php @@ -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']); } diff --git a/tests/BaseTypeTest.php b/tests/BaseTypeTest.php index aed6bafe4..d27ed6b6e 100644 --- a/tests/BaseTypeTest.php +++ b/tests/BaseTypeTest.php @@ -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 { @@ -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() {