diff --git a/src/TypesGenerator.php b/src/TypesGenerator.php index 99897159..0f0e70a5 100644 --- a/src/TypesGenerator.php +++ b/src/TypesGenerator.php @@ -51,6 +51,11 @@ class TypesGenerator */ private const SCHEMA_ORG_RANGE = 'schema:rangeIncludes'; + /** + * @var string + */ + private const SCHEMA_ORG_SUPERSEDED_BY = 'schema:supersededBy'; + /** * @var \Twig_Environment */ @@ -232,7 +237,12 @@ public function generate(array $config): void } else { // All properties foreach ($propertiesMap[$type->getUri()] as $property) { - $class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property); + if ($property->hasProperty(self::SCHEMA_ORG_SUPERSEDED_BY)) { + $supersededBy = $property->get('schema:supersededBy'); + $this->logger->warning(sprintf('The property "%s" is superseded by "%s". Using the superseding property.', $property->localName(), $supersededBy->localName())); + } else { + $class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property); + } } } diff --git a/tests/Command/GenerateTypesCommandTest.php b/tests/Command/GenerateTypesCommandTest.php index 111b1bea..79ff8ee3 100644 --- a/tests/Command/GenerateTypesCommandTest.php +++ b/tests/Command/GenerateTypesCommandTest.php @@ -368,4 +368,38 @@ public function testGeneratedEnum() $this->assertNotContains('function setId(', $gender); } + + public function testSupersededProperties() + { + $outputDir = __DIR__.'/../../build/superseded-properties'; + $config = __DIR__.'/../config/superseded-properties.yaml'; + + $this->fs->mkdir($outputDir); + + $commandTester = new CommandTester(new GenerateTypesCommand()); + $this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config])); + + $creativeWork = file_get_contents("$outputDir/AppBundle/Entity/CreativeWork.php"); + + $this->assertContains(<<<'PHP' + /** + * @var string|null an award won by or for this item + * + * @ORM\Column(type="text", nullable=true) + * @ApiProperty(iri="http://schema.org/award") + */ + private $award; +PHP + , $creativeWork); + + $this->assertNotContains(<<<'PHP' + /** + * @var string|null awards won by or for this item + * + * @ORM\Column(type="text", nullable=true) + */ + protected $award; +PHP + , $creativeWork); + } } diff --git a/tests/config/superseded-properties.yaml b/tests/config/superseded-properties.yaml new file mode 100644 index 00000000..66a48f0e --- /dev/null +++ b/tests/config/superseded-properties.yaml @@ -0,0 +1,3 @@ +types: + CreativeWork: + allProperties: true