diff --git a/.gitignore b/.gitignore index b1ffab43..40a292c2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /vendor/ /*.phar /.phpunit.result.cache +/tmp diff --git a/composer.json b/composer.json index 249016cd..4ea3a099 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "php": ">=7.4", "ext-json": "*", "doctrine/inflector": "^1.4.3 || ^2.0", - "easyrdf/easyrdf": "^0.9", + "easyrdf/easyrdf": "dev-master", "friendsofphp/php-cs-fixer": "^2.15", "league/html-to-markdown": "^4.9", "psr/log": "^1.0", @@ -32,7 +32,7 @@ }, "require-dev": { "api-platform/core": "^2.6", - "doctrine/orm": "^2.8", + "doctrine/orm": "^2.7", "myclabs/php-enum": "^1.7", "symfony/doctrine-bridge": "^5.1", "symfony/filesystem": "^5.1", diff --git a/composer.lock b/composer.lock index 7526844c..4f243f51 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3be4f27d8e131dfebc9fd1f2f84b3e60", + "content-hash": "b12162b734a74c237afbbdc4cde220e3", "packages": [ { "name": "composer/semver", @@ -360,35 +360,39 @@ }, { "name": "easyrdf/easyrdf", - "version": "0.9.x-dev", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/easyrdf/easyrdf.git", - "reference": "092952dabedd2f611fb56671c80e6f05421903cf" + "reference": "15b225d3f596061c9adbc5af193d8234f28f58fa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/easyrdf/easyrdf/zipball/092952dabedd2f611fb56671c80e6f05421903cf", - "reference": "092952dabedd2f611fb56671c80e6f05421903cf", + "url": "https://api.github.com/repos/easyrdf/easyrdf/zipball/15b225d3f596061c9adbc5af193d8234f28f58fa", + "reference": "15b225d3f596061c9adbc5af193d8234f28f58fa", "shasum": "" }, "require": { "ext-mbstring": "*", "ext-pcre": "*", - "php": ">=5.2.8" + "php": ">=7.1.0" }, "require-dev": { - "phpunit/phpunit": "~3.5", - "sami/sami": "~1.4", - "squizlabs/php_codesniffer": "~1.4.3" + "ml/json-ld": "~1.0", + "phpunit/phpunit": "^7", + "sami/sami": "^4", + "semsol/arc2": "~2.2", + "squizlabs/php_codesniffer": "3.*", + "zendframework/zend-http": "~2.3" }, "suggest": { - "ml/json-ld": "~1.0" + "ml/json-ld": "~1.0", + "semsol/arc2": "~2.2" }, "type": "library", "autoload": { - "psr-0": { - "EasyRdf_": "lib/" + "psr-4": { + "EasyRdf\\": "lib" } }, "notification-url": "https://packagist.org/downloads/", @@ -405,6 +409,7 @@ { "name": "Alexey Zakhlestin", "email": "indeyets@gmail.com", + "homepage": "http://indeyets.ru/", "role": "Developer" } ], @@ -418,7 +423,7 @@ "rdfa", "sparql" ], - "time": "2020-06-06T12:19:41+00:00" + "time": "2020-06-12T00:36:36+00:00" }, { "name": "friendsofphp/php-cs-fixer", @@ -3190,16 +3195,16 @@ }, { "name": "doctrine/orm", - "version": "2.8.x-dev", + "version": "2.7.x-dev", "source": { "type": "git", "url": "https://github.com/doctrine/orm.git", - "reference": "6bce7e9cabb2d1741dc1ebd328852bf2c48bdc61" + "reference": "93867f8d77e3a1b0c330753e627a2b8ec9b74c6f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/orm/zipball/6bce7e9cabb2d1741dc1ebd328852bf2c48bdc61", - "reference": "6bce7e9cabb2d1741dc1ebd328852bf2c48bdc61", + "url": "https://api.github.com/repos/doctrine/orm/zipball/93867f8d77e3a1b0c330753e627a2b8ec9b74c6f", + "reference": "93867f8d77e3a1b0c330753e627a2b8ec9b74c6f", "shasum": "" }, "require": { @@ -3207,15 +3212,15 @@ "doctrine/cache": "^1.9.1", "doctrine/collections": "^1.5", "doctrine/common": "^2.11 || ^3.0", - "doctrine/dbal": "^2.10.0", + "doctrine/dbal": "^2.9.3", "doctrine/event-manager": "^1.1", - "doctrine/inflector": "^1.4|^2.0", + "doctrine/inflector": "^1.0", "doctrine/instantiator": "^1.3", "doctrine/lexer": "^1.0", "doctrine/persistence": "^1.3.3 || ^2.0", "ext-pdo": "*", "ocramius/package-versions": "^1.2", - "php": "^7.2", + "php": "^7.1", "symfony/console": "^3.0|^4.0|^5.0" }, "require-dev": { @@ -3288,7 +3293,7 @@ "type": "tidelift" } ], - "time": "2020-06-03T16:40:53+00:00" + "time": "2020-06-08T21:06:53+00:00" }, { "name": "doctrine/persistence", @@ -4941,7 +4946,9 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": { + "easyrdf/easyrdf": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/AnnotationGenerator/AbstractAnnotationGenerator.php b/src/AnnotationGenerator/AbstractAnnotationGenerator.php index b1d57181..0dd53c85 100644 --- a/src/AnnotationGenerator/AbstractAnnotationGenerator.php +++ b/src/AnnotationGenerator/AbstractAnnotationGenerator.php @@ -14,6 +14,7 @@ namespace ApiPlatform\SchemaGenerator\AnnotationGenerator; use Doctrine\Inflector\Inflector; +use EasyRdf\Graph; use Psr\Log\LoggerInterface; /** @@ -34,7 +35,7 @@ abstract class AbstractAnnotationGenerator implements AnnotationGeneratorInterfa protected $logger; /** - * @var \EasyRdf_Graph[] + * @var Graph[] */ protected $graphs; diff --git a/src/AnnotationGenerator/AnnotationGeneratorInterface.php b/src/AnnotationGenerator/AnnotationGeneratorInterface.php index 3cfe114b..dac81c50 100644 --- a/src/AnnotationGenerator/AnnotationGeneratorInterface.php +++ b/src/AnnotationGenerator/AnnotationGeneratorInterface.php @@ -14,6 +14,7 @@ namespace ApiPlatform\SchemaGenerator\AnnotationGenerator; use Doctrine\Inflector\Inflector; +use EasyRdf\Graph; use Psr\Log\LoggerInterface; /** @@ -24,7 +25,7 @@ interface AnnotationGeneratorInterface { /** - * @param \EasyRdf_Graph[] $graphs + * @param Graph[] $graphs */ public function __construct(Inflector $inflector, LoggerInterface $logger, array $graphs, array $cardinalities, array $config, array $classes); diff --git a/src/AnnotationGenerator/DoctrineOrmAnnotationGenerator.php b/src/AnnotationGenerator/DoctrineOrmAnnotationGenerator.php index 73b61ac0..e2df8b4a 100644 --- a/src/AnnotationGenerator/DoctrineOrmAnnotationGenerator.php +++ b/src/AnnotationGenerator/DoctrineOrmAnnotationGenerator.php @@ -140,64 +140,78 @@ public function generateFieldAnnotations(string $className, string $fieldName): } $annotations[] = $annotation; - } elseif ($field['isEmbedded']) { + + return $annotations; + } + + $relationName = $field['range'] ? $this->getRelationName($field['range']) : null; + if ($field['isEmbedded']) { $columnPrefix = ', columnPrefix='; if (\is_bool($field['columnPrefix'])) { $columnPrefix .= $field['columnPrefix'] ? 'true' : 'false'; } else { $columnPrefix .= sprintf('"%s"', $field['columnPrefix']); } - $annotations[] = sprintf('@ORM\Embedded(class="%s"%s)', $this->getRelationName($field['range']), $columnPrefix); - } else { - switch ($field['cardinality']) { - case CardinalitiesExtractor::CARDINALITY_0_1: - $annotations[] = sprintf('@ORM\OneToOne(targetEntity="%s")', $this->getRelationName($field['range'])); - break; - case CardinalitiesExtractor::CARDINALITY_1_1: - $annotations[] = sprintf('@ORM\OneToOne(targetEntity="%s")', $this->getRelationName($field['range'])); - $annotations[] = '@ORM\JoinColumn(nullable=false)'; - break; - case CardinalitiesExtractor::CARDINALITY_UNKNOWN: - case CardinalitiesExtractor::CARDINALITY_N_0: - if ($field['inversedBy'] ?? false) { - $annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s", inversedBy="%s")', $this->getRelationName($field['range']), $field['inversedBy']); - } else { - $annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $this->getRelationName($field['range'])); - } - break; - case CardinalitiesExtractor::CARDINALITY_N_1: - if ($field['inversedBy'] ?? false) { - $annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s", inversedBy="%s")', $this->getRelationName($field['range']), $field['inversedBy']); - } else { - $annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $this->getRelationName($field['range'])); - } - $annotations[] = '@ORM\JoinColumn(nullable=false)'; - break; - case CardinalitiesExtractor::CARDINALITY_0_N: - if ($field['mappedBy'] ?? false) { - $annotations[] = sprintf('@ORM\OneToMany(targetEntity="%s", mappedBy="%s")', $this->getRelationName($field['range']), $field['mappedBy']); - } else { - $annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range'])); - } - $name = $field['relationTableName'] ? sprintf('name="%s", ', $field['relationTableName']) : ''; - $annotations[] = '@ORM\JoinTable('.$name.'inverseJoinColumns={@ORM\JoinColumn(unique=true)})'; - break; - case CardinalitiesExtractor::CARDINALITY_1_N: - if ($field['mappedBy'] ?? false) { - $annotations[] = sprintf('@ORM\OneToMany(targetEntity="%s", mappedBy="%s")', $this->getRelationName($field['range']), $field['mappedBy']); - } else { - $annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range'])); - } - $name = $field['relationTableName'] ? sprintf('name="%s", ', $field['relationTableName']) : ''; - $annotations[] = '@ORM\JoinTable('.$name.'inverseJoinColumns={@ORM\JoinColumn(nullable=false, unique=true)})'; - break; - case CardinalitiesExtractor::CARDINALITY_N_N: - $annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range'])); - if ($field['relationTableName']) { - $annotations[] = sprintf('@ORM\JoinTable(name="%s")', $field['relationTableName']); - } - break; + + if ($relationName) { + $annotations[] = sprintf('@ORM\Embedded(class="%s"%s)', $relationName, $columnPrefix); } + + return $annotations; + } + + if (!$relationName) { + return $annotations; + } + + switch ($field['cardinality']) { + case CardinalitiesExtractor::CARDINALITY_0_1: + $annotations[] = sprintf('@ORM\OneToOne(targetEntity="%s")', $relationName); + break; + case CardinalitiesExtractor::CARDINALITY_1_1: + $annotations[] = sprintf('@ORM\OneToOne(targetEntity="%s")', $relationName); + $annotations[] = '@ORM\JoinColumn(nullable=false)'; + break; + case CardinalitiesExtractor::CARDINALITY_UNKNOWN: + case CardinalitiesExtractor::CARDINALITY_N_0: + if ($field['inversedBy'] ?? false) { + $annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s", inversedBy="%s")', $relationName, $field['inversedBy']); + } else { + $annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $relationName); + } + break; + case CardinalitiesExtractor::CARDINALITY_N_1: + if ($field['inversedBy'] ?? false) { + $annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s", inversedBy="%s")', $relationName, $field['inversedBy']); + } else { + $annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $relationName); + } + $annotations[] = '@ORM\JoinColumn(nullable=false)'; + break; + case CardinalitiesExtractor::CARDINALITY_0_N: + if ($field['mappedBy'] ?? false) { + $annotations[] = sprintf('@ORM\OneToMany(targetEntity="%s", mappedBy="%s")', $relationName, $field['mappedBy']); + } else { + $annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $relationName); + } + $name = $field['relationTableName'] ? sprintf('name="%s", ', $field['relationTableName']) : ''; + $annotations[] = '@ORM\JoinTable('.$name.'inverseJoinColumns={@ORM\JoinColumn(unique=true)})'; + break; + case CardinalitiesExtractor::CARDINALITY_1_N: + if ($field['mappedBy'] ?? false) { + $annotations[] = sprintf('@ORM\OneToMany(targetEntity="%s", mappedBy="%s")', $relationName, $field['mappedBy']); + } else { + $annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $relationName); + } + $name = $field['relationTableName'] ? sprintf('name="%s", ', $field['relationTableName']) : ''; + $annotations[] = '@ORM\JoinTable('.$name.'inverseJoinColumns={@ORM\JoinColumn(nullable=false, unique=true)})'; + break; + case CardinalitiesExtractor::CARDINALITY_N_N: + $annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $relationName); + if ($field['relationTableName']) { + $annotations[] = sprintf('@ORM\JoinTable(name="%s")', $field['relationTableName']); + } + break; } return $annotations; @@ -243,8 +257,12 @@ private function generateIdAnnotations(): array /** * Gets class or interface name to use in relations. */ - private function getRelationName(string $range): string + private function getRelationName(string $range): ?string { + if (!isset($this->classes[$range])) { + return null; + } + $class = $this->classes[$range]; if (isset($class['interfaceName'])) { diff --git a/src/CardinalitiesExtractor.php b/src/CardinalitiesExtractor.php index 963a0f23..78143e1b 100644 --- a/src/CardinalitiesExtractor.php +++ b/src/CardinalitiesExtractor.php @@ -13,6 +13,9 @@ namespace ApiPlatform\SchemaGenerator; +use EasyRdf\Graph; +use EasyRdf\Resource; + /** * Cardinality extractor. * @@ -30,14 +33,14 @@ class CardinalitiesExtractor public const CARDINALITY_UNKNOWN = 'unknown'; /** - * @var \EasyRdf_Graph[] + * @var Graph[] */ private $graphs; private $goodRelationsBridge; /** - * @param \EasyRdf_Graph[] $graphs + * @param Graph[] $graphs */ public function __construct(array $graphs, GoodRelationsBridge $goodRelationsBridge) { @@ -68,7 +71,7 @@ public function extract(): array * * @return string The cardinality */ - private function extractForProperty(\EasyRdf_Resource $property): string + private function extractForProperty(Resource $property): string { $localName = $property->localName(); $fromGoodRelations = $this->goodRelationsBridge->extractCardinality($localName); diff --git a/src/Command/ExtractCardinalitiesCommand.php b/src/Command/ExtractCardinalitiesCommand.php index aa285d48..e327f0f5 100644 --- a/src/Command/ExtractCardinalitiesCommand.php +++ b/src/Command/ExtractCardinalitiesCommand.php @@ -16,6 +16,7 @@ use ApiPlatform\SchemaGenerator\CardinalitiesExtractor; use ApiPlatform\SchemaGenerator\GoodRelationsBridge; use ApiPlatform\SchemaGenerator\TypesGeneratorConfiguration; +use EasyRdf\Graph; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -36,8 +37,8 @@ protected function configure(): void $this ->setName('extract-cardinalities') ->setDescription('Extract properties\' cardinality') - ->addOption('schemaorg-file', 's', InputOption::VALUE_REQUIRED, 'The path or URL of the Schema.org RDFa file to use.', TypesGeneratorConfiguration::SCHEMA_ORG_RDFA_URL) - ->addOption('goodrelations-file', 'g', InputOption::VALUE_REQUIRED, 'The path or URL of the GoodRelations OWL file to use.', TypesGeneratorConfiguration::GOOD_RELATIONS_OWL_URL) + ->addOption('schemaorg-file', 's', InputOption::VALUE_REQUIRED, 'The path or URL of the Schema.org RDF file to use.', TypesGeneratorConfiguration::SCHEMA_ORG_URI) + ->addOption('goodrelations-file', 'g', InputOption::VALUE_REQUIRED, 'The path or URL of the GoodRelations OWL file to use.', TypesGeneratorConfiguration::GOOD_RELATIONS_URI) ; } @@ -49,11 +50,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $schemaOrgFile = $input->getOption('schemaorg-file'); $relations = []; - $schemaOrg = new \EasyRdf_Graph(); - if ('http://' === substr($schemaOrgFile, 0, 7) || 'https://' === substr($schemaOrgFile, 0, 8)) { - $schemaOrg->load($input->getOption('schemaorg-file'), 'rdfa'); + $schemaOrg = new Graph(); + + $format = pathinfo($schemaOrgFile, PATHINFO_EXTENSION) ?: 'guess'; + if (0 === strpos($schemaOrgFile, 'http://') || 0 === strpos($schemaOrgFile, 'https://')) { + $schemaOrg->load($input->getOption('schemaorg-file'), $format); } else { - $schemaOrg->parseFile($input->getOption('schemaorg-file'), 'rdfa'); + $schemaOrg->parseFile($input->getOption('schemaorg-file'), $format); } $relations[] = $schemaOrg; diff --git a/src/Command/GenerateTypesCommand.php b/src/Command/GenerateTypesCommand.php index 943d572d..407909a9 100644 --- a/src/Command/GenerateTypesCommand.php +++ b/src/Command/GenerateTypesCommand.php @@ -18,6 +18,7 @@ use ApiPlatform\SchemaGenerator\TypesGenerator; use ApiPlatform\SchemaGenerator\TypesGeneratorConfiguration; use Doctrine\Inflector\InflectorFactory; +use EasyRdf\Graph; use Symfony\Component\Config\Definition\Processor; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -139,12 +140,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $graphs = []; - foreach ($processedConfiguration['rdfa'] as $rdfa) { - $graph = new \EasyRdf_Graph(); - if (0 === strpos($rdfa['uri'], 'http://') || 0 === strpos($rdfa['uri'], 'https://')) { - $graph->load($rdfa['uri'], $rdfa['format']); + foreach ($processedConfiguration['vocabs'] as $vocab) { + $graph = new Graph(); + if (0 === strpos($vocab['uri'], 'http://') || 0 === strpos($vocab['uri'], 'https://')) { + $graph->load($vocab['uri'], $vocab['format']); } else { - $graph->parseFile($rdfa['uri'], $rdfa['format']); + $graph->parseFile($vocab['uri'], $vocab['format']); } $graphs[] = $graph; diff --git a/src/TypesGenerator.php b/src/TypesGenerator.php index d9faa5c5..6ada2eb0 100644 --- a/src/TypesGenerator.php +++ b/src/TypesGenerator.php @@ -17,6 +17,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Inflector\Inflector; +use EasyRdf\Graph; +use EasyRdf\Resource; use MyCLabs\Enum\Enum; use PhpCsFixer\Cache\NullCacheManager; use PhpCsFixer\Differ\NullDiffer; @@ -68,7 +70,7 @@ class TypesGenerator private $logger; /** - * @var \EasyRdf_Graph[] + * @var Graph[] */ private $graphs; @@ -88,7 +90,7 @@ class TypesGenerator private $inflector; /** - * @param \EasyRdf_Graph[] $graphs + * @param Graph[] $graphs */ public function __construct(Inflector $inflector, Environment $twig, LoggerInterface $logger, array $graphs, CardinalitiesExtractor $cardinalitiesExtractor, GoodRelationsBridge $goodRelationsBridge) { @@ -459,7 +461,7 @@ public function generate(array $config): void /** * Tests if a type is an enum. */ - private function isEnum(\EasyRdf_Resource $type): bool + private function isEnum(Resource $type): bool { $subClassOf = $type->get('rdfs:subClassOf'); @@ -471,7 +473,7 @@ private function isEnum(\EasyRdf_Resource $type): bool * * @param string[] $parentClasses */ - private function getParentClasses(\EasyRdf_Resource $resource, array $parentClasses = []): array + private function getParentClasses(Resource $resource, array $parentClasses = []): array { if ([] === $parentClasses) { return $this->getParentClasses($resource, [$resource->getUri()]); @@ -575,13 +577,8 @@ private function fieldToAdderRemoverTypeHint(array $field, array $classes): ?str /** * Updates generated $class with given field config. - * - * @param string $typeName - * @param string $propertyName - * - * @return array $class */ - private function generateField(array $config, array $class, \EasyRdf_Resource $type, $typeName, $propertyName, \EasyRdf_Resource $property = null): array + private function generateField(array $config, array $class, Resource $type, string $typeName, string $propertyName, ?Resource $property = null): array { $typeConfig = $config['types'][$typeName] ?? null; $typesDefined = !empty($config['types']); @@ -594,7 +591,7 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t } // Ignore or warn when properties are legacy - if (!empty($property) && preg_match('/legacy spelling/', (string) $property->get('rdfs:comment'))) { + if (null !== $property && preg_match('/legacy spelling/', (string) $property->get('rdfs:comment'))) { if (isset($typeConfig['properties'])) { $this->logger->warning(sprintf('The property "%s" (type "%s") is legacy.', $propertyName, $type->localName())); } else { @@ -604,15 +601,24 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t } } - $propertyConfig = $typeConfig['properties'][$propertyName] ?? null; - + $propertyConfig = $typeConfig['properties'][$propertyName] ?? []; $ranges = []; - if (isset($propertyConfig['range']) && $propertyConfig['range']) { + + $isCustom = true; + if ($propertyConfig['range'] ?? false) { $ranges[] = $propertyConfig['range']; - } elseif (!empty($property)) { + } + + if (null !== $property) { foreach ($property->all(self::SCHEMA_ORG_RANGE) as $range) { - if (!$typesDefined || $this->isDatatype($range->localName()) || isset($config['types'][$range->localName()])) { - $ranges[] = $range->localName(); + $localName = $range->localName(); + if (!$typesDefined || isset($config['types'][$localName]) || $this->isDatatype($localName)) { + if (($propertyConfig['range'] ?? null) === $localName) { + $isCustom = false; + break; + } + $isCustom = false; + $ranges[] = $localName; } } } @@ -663,7 +669,7 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t 'isWritable' => $propertyConfig['writable'] ?? true, 'isNullable' => $isNullable, 'isUnique' => isset($propertyConfig['unique']) && $propertyConfig['unique'], - 'isCustom' => empty($property), + 'isCustom' => $isCustom, 'isEmbedded' => $isEmbedded, 'columnPrefix' => $columnPrefix, 'mappedBy' => $propertyConfig['mappedBy'] ?? null, diff --git a/src/TypesGeneratorConfiguration.php b/src/TypesGeneratorConfiguration.php index df2f9a1c..cdb2927c 100644 --- a/src/TypesGeneratorConfiguration.php +++ b/src/TypesGeneratorConfiguration.php @@ -29,14 +29,14 @@ final class TypesGeneratorConfiguration implements ConfigurationInterface { /** - * @see https://schema.org/docs/schema_org_rdfa.html + * @see https://schema.org/version/latest/schema.rdf */ - public const SCHEMA_ORG_RDFA_URL = __DIR__.'/../data/schema.rdfa'; + public const SCHEMA_ORG_URI = 'https://schema.org/version/latest/schema.rdf'; /** * @see https://purl.org/goodrelations/v1.owl */ - public const GOOD_RELATIONS_OWL_URL = __DIR__.'/../data/v1.owl'; + public const GOOD_RELATIONS_URI = __DIR__.'/../data/v1.owl'; public const SCHEMA_ORG_NAMESPACE = 'http://schema.org/'; private $defaultPrefix; @@ -58,15 +58,15 @@ public function getConfigTreeBuilder(): TreeBuilder $treeBuilder ->getRootNode() ->children() - ->arrayNode('rdfa') - ->info('RDFa files') - ->defaultValue([['uri' => self::SCHEMA_ORG_RDFA_URL, 'format' => 'rdfa']]) + ->arrayNode('vocabs') + ->info('RDF vocabularies') + ->defaultValue([['uri' => self::SCHEMA_ORG_URI, 'format' => 'rdfxml']]) ->beforeNormalization() ->ifArray() - ->then(function (array $v) { + ->then(static function (array $v) { return array_map( - function ($rdfa) { - return is_scalar($rdfa) ? ['uri' => $rdfa, 'format' => null] : $rdfa; + static function ($rdf) { + return is_scalar($rdf) ? ['uri' => $rdf, 'format' => null] : $rdf; }, $v ); @@ -74,15 +74,15 @@ function ($rdfa) { ->end() ->arrayPrototype() ->children() - ->scalarNode('uri')->defaultValue(self::SCHEMA_ORG_RDFA_URL)->info('RDFa URI to use')->example('https://schema.org/docs/schema_org_rdfa.html')->end() - ->scalarNode('format')->defaultNull()->info('RDFa URI data format')->example('rdfxml')->end() + ->scalarNode('uri')->defaultValue(self::SCHEMA_ORG_URI)->info('RDF vocabulary to use')->example('https://schema.org/version/latest/all-layers.rdf')->end() + ->scalarNode('format')->defaultValue('guess')->info('RDF vocabulary format')->example('rdfxml')->end() ->end() ->end() ->end() ->arrayNode('relations') ->info('OWL relation files to use') ->example('https://purl.org/goodrelations/v1.owl') - ->defaultValue([self::GOOD_RELATIONS_OWL_URL]) + ->defaultValue([self::GOOD_RELATIONS_URI]) ->prototype('scalar')->end() ->end() ->booleanNode('debug')->defaultFalse()->info('Debug mode')->end() diff --git a/tests/AnnotationGenerator/ApiPlatformCoreAnnotationGeneratorTest.php b/tests/AnnotationGenerator/ApiPlatformCoreAnnotationGeneratorTest.php index ce41cdf2..953503f8 100644 --- a/tests/AnnotationGenerator/ApiPlatformCoreAnnotationGeneratorTest.php +++ b/tests/AnnotationGenerator/ApiPlatformCoreAnnotationGeneratorTest.php @@ -16,6 +16,8 @@ use ApiPlatform\SchemaGenerator\AnnotationGenerator\ApiPlatformCoreAnnotationGenerator; use ApiPlatform\SchemaGenerator\TypesGenerator; use Doctrine\Inflector\InflectorFactory; +use EasyRdf\Graph; +use EasyRdf\Resource; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; @@ -31,8 +33,8 @@ class ApiPlatformCoreAnnotationGeneratorTest extends TestCase protected function setUp(): void { - $graph = new \EasyRdf_Graph(); - $myEnum = new \EasyRdf_Resource('http://schema.org/MyEnum', $graph); + $graph = new Graph(); + $myEnum = new Resource('http://schema.org/MyEnum', $graph); $myEnum->add('rdfs:subClassOf', ['type' => 'uri', 'value' => TypesGenerator::SCHEMA_ORG_ENUMERATION]); $this->generator = new ApiPlatformCoreAnnotationGenerator( @@ -43,7 +45,7 @@ protected function setUp(): void [], [ 'Res' => [ - 'resource' => new \EasyRdf_Resource('http://schema.org/Res', $graph), + 'resource' => new Resource('http://schema.org/Res', $graph), 'fields' => [ 'prop' => ['isCustom' => false], 'customProp' => ['isCustom' => true], @@ -51,7 +53,7 @@ protected function setUp(): void ], 'MyEnum' => ['resource' => $myEnum], 'WithOperations' => [ - 'resource' => new \EasyRdf_Resource('http://schema.org/WithOperations', $graph), + 'resource' => new Resource('http://schema.org/WithOperations', $graph), 'operations' => [ 'item' => ['get' => ['route_name' => 'api_about_get']], 'collection' => [], diff --git a/tests/AnnotationGenerator/DoctrineOrmAnnotationGeneratorTest.php b/tests/AnnotationGenerator/DoctrineOrmAnnotationGeneratorTest.php index 8d88693e..206104d1 100644 --- a/tests/AnnotationGenerator/DoctrineOrmAnnotationGeneratorTest.php +++ b/tests/AnnotationGenerator/DoctrineOrmAnnotationGeneratorTest.php @@ -16,6 +16,8 @@ use ApiPlatform\SchemaGenerator\AnnotationGenerator\DoctrineOrmAnnotationGenerator; use ApiPlatform\SchemaGenerator\TypesGenerator; use Doctrine\Inflector\InflectorFactory; +use EasyRdf\Graph; +use EasyRdf\Resource; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; @@ -31,8 +33,8 @@ class DoctrineOrmAnnotationGeneratorTest extends TestCase protected function setUp(): void { - $graph = new \EasyRdf_Graph(); - $myEnum = new \EasyRdf_Resource('https://schema.org/MyEnum', $graph); + $graph = new Graph(); + $myEnum = new Resource('https://schema.org/MyEnum', $graph); $myEnum->add('rdfs:subClassOf', ['type' => 'uri', 'value' => TypesGenerator::SCHEMA_ORG_ENUMERATION]); $this->generator = new DoctrineOrmAnnotationGenerator( InflectorFactory::create()->build(), @@ -43,7 +45,7 @@ protected function setUp(): void [ 'Product' => [ 'isEnum' => false, - 'resource' => new \EasyRdf_Resource('https://schema.org/Product', $graph), + 'resource' => new Resource('https://schema.org/Product', $graph), 'abstract' => true, 'embeddable' => false, ], @@ -51,7 +53,7 @@ protected function setUp(): void 'isEnum' => false, 'abstract' => false, 'embeddable' => false, - 'resource' => new \EasyRdf_Resource('https://schema.org/Vehicle', $graph), + 'resource' => new Resource('https://schema.org/Vehicle', $graph), 'fields' => [ 'weight' => [ 'isEnum' => false, @@ -73,7 +75,7 @@ protected function setUp(): void 'isEnum' => false, 'abstract' => false, 'name' => 'QuantitativeValue', - 'resource' => new \EasyRdf_Resource('https://schema.org/QuantitativeValue', $graph), + 'resource' => new Resource('https://schema.org/QuantitativeValue', $graph), 'embeddable' => true, ], 'MyEnum' => ['isEnum' => true, 'resource' => $myEnum], diff --git a/tests/Command/DumpConfigurationTest.php b/tests/Command/DumpConfigurationTest.php index 76f70323..f0d47d56 100644 --- a/tests/Command/DumpConfigurationTest.php +++ b/tests/Command/DumpConfigurationTest.php @@ -31,17 +31,17 @@ public function testDumpConfiguration(): void <<<'YAML' config: - # RDFa files - rdfa: + # RDF vocabularies + vocabs: # Prototype - - # RDFa URI to use - uri: %s # Example: https://schema.org/docs/schema_org_rdfa.html + # RDF vocabulary to use + uri: 'https://schema.org/version/latest/schema.rdf' # Example: https://schema.org/version/latest/all-layers.rdf - # RDFa URI data format - format: null # Example: rdfxml + # RDF vocabulary format + format: guess # Example: rdfxml # OWL relation files to use relations: # Example: https://purl.org/goodrelations/v1.owl @@ -211,7 +211,6 @@ interface: null YAML , - str_replace('generator/data', 'generator/src/../data', realpath(__DIR__.'/../../data/schema.rdfa')), str_replace('generator/data', 'generator/src/../data', realpath(__DIR__.'/../../data/v1.owl')) ), $commandTester->getDisplay() diff --git a/tests/TypesGeneratorTest.php b/tests/TypesGeneratorTest.php index a77389ba..f50df15d 100644 --- a/tests/TypesGeneratorTest.php +++ b/tests/TypesGeneratorTest.php @@ -18,6 +18,7 @@ use ApiPlatform\SchemaGenerator\TypesGenerator; use ApiPlatform\SchemaGenerator\TypesGeneratorConfiguration; use Doctrine\Inflector\InflectorFactory; +use EasyRdf\Graph; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Psr\Log\NullLogger; @@ -52,7 +53,7 @@ public function testGenerate(): void private function getGraphs(): array { - $graph = new \EasyRdf_Graph(); + $graph = new Graph(); $graph->addResource('http://schema.org/Article', 'rdf:type', 'rdfs:Class'); $graph->addResource('http://schema.org/Article', 'rdfs:subClassOf', 'http://schema.org/CreativeWork'); diff --git a/tests/config/vgo.yaml b/tests/config/vgo.yaml index 7f7343b9..d2ad7de1 100644 --- a/tests/config/vgo.yaml +++ b/tests/config/vgo.yaml @@ -1,4 +1,7 @@ -rdfa: [tests/data/vgo.rdf, data/schema.rdfa] +vocabs: + - tests/data/vgo.rdf + - uri: data/schema.rdfa + format: rdfa annotationGenerators: - ApiPlatform\SchemaGenerator\AnnotationGenerator\PhpDocAnnotationGenerator diff --git a/tests/e2e/schema.yml b/tests/e2e/schema.yml index 050799e3..7678a59e 100644 --- a/tests/e2e/schema.yml +++ b/tests/e2e/schema.yml @@ -11,19 +11,16 @@ types: familyName: ~ givenName: ~ additionalName: ~ - gender: ~ address: ~ birthDate: ~ telephone: ~ email: ~ url: ~ - jobTitle: ~ PostalAddress: # Disable the generation of the class hierarchy for this type parent: false properties: - # Force the type of the addressCountry property to text - addressCountry: { range: "Text" } + addressCountry: { range: Text } addressLocality: ~ addressRegion: ~ postOfficeBoxNumber: ~ diff --git a/tests/e2e/src/App/Entity/Person.php b/tests/e2e/src/App/Entity/Person.php index 23d6ba5c..fb048ddd 100644 --- a/tests/e2e/src/App/Entity/Person.php +++ b/tests/e2e/src/App/Entity/Person.php @@ -52,14 +52,6 @@ class Person */ private $additionalName; - /** - * @var string|null Gender of the person. While http://schema.org/Male and http://schema.org/Female may be used, text strings are also acceptable for people who do not identify as a binary gender. - * - * @ORM\Column(type="text", nullable=true) - * @ApiProperty(iri="http://schema.org/gender") - */ - private $gender; - /** * @var PostalAddress|null physical address of the item * @@ -103,14 +95,6 @@ class Person */ private $url; - /** - * @var string|null the job title of the person (for example, Financial Manager) - * - * @ORM\Column(type="text", nullable=true) - * @ApiProperty(iri="http://schema.org/jobTitle") - */ - private $jobTitle; - public function getId(): ?int { return $this->id; @@ -146,16 +130,6 @@ public function getAdditionalName(): ?string return $this->additionalName; } - public function setGender(?string $gender): void - { - $this->gender = $gender; - } - - public function getGender(): ?string - { - return $this->gender; - } - public function setAddress(?PostalAddress $address): void { $this->address = $address; @@ -205,14 +179,4 @@ public function getUrl(): ?string { return $this->url; } - - public function setJobTitle(?string $jobTitle): void - { - $this->jobTitle = $jobTitle; - } - - public function getJobTitle(): ?string - { - return $this->jobTitle; - } } diff --git a/tests/e2e/src/App/Entity/PostalAddress.php b/tests/e2e/src/App/Entity/PostalAddress.php index 2077f916..5347510a 100644 --- a/tests/e2e/src/App/Entity/PostalAddress.php +++ b/tests/e2e/src/App/Entity/PostalAddress.php @@ -28,7 +28,7 @@ class PostalAddress private $id; /** - * @var string|null The country. For example, USA. You can also provide the two-letter \[ISO 3166-1 alpha-2 country code\](http://en.wikipedia.org/wiki/ISO\_3166-1). + * @var string|null The country. For example, USA. You can also provide the two-letter [ISO 3166-1 alpha-2 country code](http://en.wikipedia.org/wiki/ISO_3166-1). * * @ORM\Column(type="text", nullable=true) * @ApiProperty(iri="http://schema.org/addressCountry") @@ -36,7 +36,7 @@ class PostalAddress private $addressCountry; /** - * @var string|null The locality. For example, Mountain View. + * @var string|null The locality in which the street address is, and which is in the region. For example, Mountain View. * * @ORM\Column(type="text", nullable=true) * @ApiProperty(iri="http://schema.org/addressLocality") @@ -44,7 +44,7 @@ class PostalAddress private $addressLocality; /** - * @var string|null The region. For example, CA. + * @var string|null The region in which the locality is, and which is in the country. For example, California or another appropriate first-level [Administrative division](https://en.wikipedia.org/wiki/List_of_administrative_divisions_by_country) * * @ORM\Column(type="text", nullable=true) * @ApiProperty(iri="http://schema.org/addressRegion")