From 51b65513ca04899ad94c3223b2c5bc00babc2a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 2 Jun 2016 09:31:40 +0200 Subject: [PATCH] Make title and description config parameters optional --- .../DependencyInjection/Configuration.php | 4 +- src/Hydra/ApiDocumentationBuilder.php | 63 +++++-------------- 2 files changed, 17 insertions(+), 50 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php index 428c80c0c72..6bc6b6f4928 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php @@ -31,8 +31,8 @@ public function getConfigTreeBuilder() $rootNode ->children() - ->scalarNode('title')->cannotBeEmpty()->isRequired()->info('The title of the API.')->end() - ->scalarNode('description')->cannotBeEmpty()->isRequired()->info('The description of the API.')->end() + ->scalarNode('title')->defaultValue('')->info('The title of the API.')->end() + ->scalarNode('description')->defaultValue('')->info('The description of the API.')->end() ->arrayNode('supported_formats') ->defaultValue(['jsonld' => ['mime_types' => ['application/ld+json']]]) ->info('The list of enabled formats. The first one will be the default.') diff --git a/src/Hydra/ApiDocumentationBuilder.php b/src/Hydra/ApiDocumentationBuilder.php index debd7a0a1b0..36ee162ab66 100644 --- a/src/Hydra/ApiDocumentationBuilder.php +++ b/src/Hydra/ApiDocumentationBuilder.php @@ -29,57 +29,18 @@ */ final class ApiDocumentationBuilder implements ApiDocumentationBuilderInterface { - /** - * @var ResourceNameCollectionFactoryInterface - */ private $resourceNameCollectionFactory; - - /** - * @var ResourceMetadataFactoryInterface - */ private $resourceMetadataFactory; - - /** - * @var PropertyNameCollectionFactoryInterface - */ private $propertyNameCollectionFactory; - - /** - * @var PropertyMetadataFactoryInterface - */ private $propertyMetadataFactory; - - /** - * @var ContextBuilderInterface - */ private $contextBuilder; - - /** - * @var ResourceClassResolverInterface - */ private $resourceClassResolver; - - /** - * @var OperationMethodResolverInterface - */ private $operationMethodResolver; - - /** - * @var UrlGeneratorInterface - */ private $urlGenerator; - - /** - * @var string - */ private $title; - - /** - * @var string - */ private $description; - public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, string $title, string $description) + public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, UrlGeneratorInterface $urlGenerator, string $title = '', string $description = '') { $this->resourceNameCollectionFactory = $resourceNameCollectionFactory; $this->resourceMetadataFactory = $resourceMetadataFactory; @@ -275,14 +236,20 @@ public function getApiDocumentation() ], ]; - return [ - '@context' => $this->getContext(), - '@id' => $this->urlGenerator->generate('api_hydra_vocab'), - 'hydra:title' => $this->title, - 'hydra:description' => $this->description, - 'hydra:entrypoint' => $this->urlGenerator->generate('api_jsonld_entrypoint'), - 'hydra:supportedClass' => $classes, - ]; + $doc = ['@context' => $this->getContext(), '@id' => $this->urlGenerator->generate('api_hydra_vocab')]; + + if ('' !== $this->title) { + $doc['hydra:title'] = $this->title; + } + + if ('' !== $this->description) { + $doc['hydra:description'] = $this->description; + } + + $doc['hydra:entrypoint'] = $this->urlGenerator->generate('api_jsonld_entrypoint'); + $doc['hydra:supportedClass'] = $classes; + + return $doc; } /**