From f74b79dbdacd17651ffea84cfda144c1f1846e87 Mon Sep 17 00:00:00 2001 From: Florent Mata Date: Tue, 20 Jun 2017 17:18:40 +0200 Subject: [PATCH] [Hydra] add collection flag in documentation properties --- .../Serializer/DocumentationNormalizer.php | 30 ++++++++++++++----- .../DocumentationNormalizerTest.php | 10 ++++++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Hydra/Serializer/DocumentationNormalizer.php b/src/Hydra/Serializer/DocumentationNormalizer.php index 523fc5ad1ac..9cd55f6ad3e 100644 --- a/src/Hydra/Serializer/DocumentationNormalizer.php +++ b/src/Hydra/Serializer/DocumentationNormalizer.php @@ -95,7 +95,15 @@ private function populateEntrypointProperties(string $resourceClass, ResourceMet '@type' => 'hydra:Link', 'domain' => '#Entrypoint', 'rdfs:label' => "The collection of $shortName resources", - 'range' => 'hydra:PagedCollection', + 'rdfs:range' => [ + 'hydra:PagedCollection', + [ + 'owl:equivalentClass' => [ + 'owl:onProperty' => 'hydra:member', + 'owl:allValuesFrom' => "#$shortName", + ], + ], + ], 'hydra:supportedOperation' => $hydraCollectionOperations, ], 'hydra:title' => "The collection of $shortName resources", @@ -423,14 +431,22 @@ private function getClasses(array $entrypointProperties, array $classes): array */ private function getProperty(PropertyMetadata $propertyMetadata, string $propertyName, string $prefixedShortName, string $shortName): array { + $propertyData = [ + '@id' => $propertyMetadata->getIri() ?? "#$shortName/$propertyName", + '@type' => $propertyMetadata->isReadableLink() ? 'rdf:Property' : 'hydra:Link', + 'rdfs:label' => $propertyName, + 'domain' => $prefixedShortName, + ]; + + $type = $propertyMetadata->getType(); + + if (null !== $type && !$type->isCollection() && (null !== $className = $type->getClassName()) && $this->resourceClassResolver->isResourceClass($className)) { + $propertyData['owl:maxCardinality'] = 1; + } + $property = [ '@type' => 'hydra:SupportedProperty', - 'hydra:property' => [ - '@id' => $propertyMetadata->getIri() ?? "#$shortName/$propertyName", - '@type' => $propertyMetadata->isReadableLink() ? 'rdf:Property' : 'hydra:Link', - 'rdfs:label' => $propertyName, - 'domain' => $prefixedShortName, - ], + 'hydra:property' => $propertyData, 'hydra:title' => $propertyName, 'hydra:required' => $propertyMetadata->isRequired(), 'hydra:readable' => $propertyMetadata->isReadable(), diff --git a/tests/Hydra/Serializer/DocumentationNormalizerTest.php b/tests/Hydra/Serializer/DocumentationNormalizerTest.php index e88bc6fcb81..e12029be904 100644 --- a/tests/Hydra/Serializer/DocumentationNormalizerTest.php +++ b/tests/Hydra/Serializer/DocumentationNormalizerTest.php @@ -175,7 +175,15 @@ public function testNormalize() '@type' => 'hydra:Link', 'rdfs:label' => 'The collection of dummy resources', 'domain' => '#Entrypoint', - 'range' => 'hydra:PagedCollection', + 'rdfs:range' => [ + 'hydra:PagedCollection', + [ + 'owl:equivalentClass' => [ + 'owl:onProperty' => 'hydra:member', + 'owl:allValuesFrom' => '#dummy', + ], + ], + ], 'hydra:supportedOperation' => [ 0 => [ '@type' => 'hydra:Operation',