diff --git a/generator/templates/static/BaseType.php b/generator/templates/static/BaseType.php index dcb57d351..cb5dd51ae 100644 --- a/generator/templates/static/BaseType.php +++ b/generator/templates/static/BaseType.php @@ -81,6 +81,7 @@ public function offsetUnset($offset) public function toArray(): array { + $this->serializeIdentifier(); $properties = $this->serializeProperty($this->getProperties()); return [ @@ -115,6 +116,14 @@ protected function serializeProperty($property) return $property; } + protected function serializeIdentifier() + { + if (isset($this['identifier'])) { + $this->setProperty('@id', $this['identifier']); + unset($this['identifier']); + } + } + public function toScript(): string { return ''; diff --git a/src/BaseType.php b/src/BaseType.php index dcb57d351..cb5dd51ae 100644 --- a/src/BaseType.php +++ b/src/BaseType.php @@ -81,6 +81,7 @@ public function offsetUnset($offset) public function toArray(): array { + $this->serializeIdentifier(); $properties = $this->serializeProperty($this->getProperties()); return [ @@ -115,6 +116,14 @@ protected function serializeProperty($property) return $property; } + protected function serializeIdentifier() + { + if (isset($this['identifier'])) { + $this->setProperty('@id', $this['identifier']); + unset($this['identifier']); + } + } + public function toScript(): string { return ''; diff --git a/tests/BaseTypeTest.php b/tests/BaseTypeTest.php index 41dece018..a31934c9a 100644 --- a/tests/BaseTypeTest.php +++ b/tests/BaseTypeTest.php @@ -308,6 +308,22 @@ public function it_can_be_casted_to_string() $this->assertEquals($expected, (string) $type); } + + /** @test */ + public function it_replaces_identifier_with_at_id_property() + { + $type = new DummyType(); + + $type->setProperty('identifier', 'object#1'); + + $expected = [ + '@context' => 'https://schema.org', + '@type' => 'DummyType', + '@id' => 'object#1', + ]; + + $this->assertEquals($expected, $type->toArray()); + } } class DummyType extends BaseType