/** * Builds the JSON-LD context for the given resource. * * @param ResourceInterface|null $resource * * @return array */ public function getContext(ResourceInterface $resource = null) { $context = parent::getContext($resource); if ($resource) { $normalizedOutput = $this->documentationHelper->normalizeClassParameter($resource->getEntityClass(), $resource); $data = $this->documentationHelper->getParametersParser($normalizedOutput, $resource); $embeds = $this->documentationHelper->transformerHelper->getAvailableIncludes($resource->getShortName()); $context['@embed'] = implode(',', $embeds); foreach ($data as $key => $value) { $context[$key] = '#' . $resource->getShortName() . '/' . $key; } } return $context; }
/** * {@inheritdoc} */ public function getApiDocumentation() { $classes = []; $entrypointProperties = []; foreach ($this->resourceCollection as $resource) { $shortName = $resource->getShortName(); $prefixedShortName = '#' . $shortName; $collectionOperations = []; foreach ($resource->getCollectionOperations() as $collectionOperation) { $collectionOperations[] = $this->getHydraOperation($resource, $collectionOperation, $prefixedShortName, true); } $class = ['@id' => $prefixedShortName, '@type' => 'hydra:Class', 'rdfs:label' => $resource->getShortName(), 'hydra:title' => $resource->getShortName(), 'hydra:description' => 'Object ' . $resource->getShortName()]; if (!empty($resource->getAlias())) { $class['hydra:description'] = $class['hydra:description'] . ' [alias: ' . implode(',', $resource->getAlias()) . ']'; } if (!is_null($resource->getParent())) { $class['hydra:description'] = $class['hydra:description'] . ' (parent: ' . $resource->getParent()->getShortName() . ')'; } $properties = []; $normalizedOutput = $this->documentationHelper->normalizeClassParameter($resource->getEntityClass(), $resource); $attributes = $this->documentationHelper->getParametersParser($normalizedOutput, $resource->getShortName()); foreach ($attributes as $attributeName => $attributeMetadata) { $type = 'rdf:Property'; $property = ['@type' => 'hydra:SupportedProperty', 'hydra:property' => ['@id' => sprintf('#%s/%s', $shortName, $attributeName), '@type' => $type, 'rdfs:label' => $attributeName, 'domain' => $prefixedShortName], 'hydra:title' => $attributeName, 'hydra:required' => $attributeMetadata['required']]; if ($range = $this->getRange($attributeMetadata)) { $property['hydra:property']['range'] = $range; } if (isset($attributeMetadata['description'])) { $property['hydra:description'] = $description = $attributeMetadata['description']; } $properties[] = $property; } $class['hydra:supportedProperty'] = $properties; $operations = []; foreach ($resource->getItemOperations() as $itemOperation) { $operations[] = $this->getHydraOperation($resource, $itemOperation, $prefixedShortName, false); } $class['hydra:supportedOperation'] = array_merge($operations, $collectionOperations); $classes[] = $class; } // Entrypoint $classes[] = ['@id' => '#Entrypoint', '@type' => 'hydra:Class', 'hydra:title' => 'The API entrypoint', 'hydra:supportedProperty' => $entrypointProperties, 'hydra:supportedOperation' => ['@type' => 'hydra:Operation', 'hydra:method' => 'GET', 'rdfs:label' => 'The API entrypoint.', 'returns' => '#EntryPoint']]; return ['@context' => $this->getContext(), '@id' => $this->router->generate('api_hydra_vocab'), 'hydra:title' => $this->title, 'hydra:description' => $this->description, 'hydra:entrypoint' => $this->router->generate('api_json_ld_entrypoint'), 'hydra:supportedClass' => $classes]; }