From f8e65799ee7bd1a0df597e8d2d997d107616c484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 16 Oct 2017 21:06:11 +0200 Subject: [PATCH 1/2] Sort use statements --- core/data-providers.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/data-providers.md b/core/data-providers.md index 93a0efaf24c..96b71bf0f77 100644 --- a/core/data-providers.md +++ b/core/data-providers.md @@ -34,9 +34,9 @@ If no data is available, you should return an empty array. namespace AppBundle\DataProvider; -use AppBundle\Entity\BlogPost; use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; +use AppBundle\Entity\BlogPost; final class BlogPostCollectionDataProvider implements CollectionDataProviderInterface { @@ -84,9 +84,9 @@ The `getItem` method can return `null` if no result has been found. namespace AppBundle\DataProvider; -use AppBundle\Entity\BlogPost; use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; +use AppBundle\Entity\BlogPost; final class BlogPostItemDataProvider implements ItemDataProviderInterface { From ad077dc409462e8781bc3b61bded1f5bcb7b8b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 16 Oct 2017 21:24:13 +0200 Subject: [PATCH 2/2] Document RestrictedDataProviderInterface usage --- core/data-providers.md | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/core/data-providers.md b/core/data-providers.md index 96b71bf0f77..8c58bb0830f 100644 --- a/core/data-providers.md +++ b/core/data-providers.md @@ -17,6 +17,10 @@ For a given resource, you can implement two kind of interfaces: * the [`ItemDataProviderInterface`](https://github.com/api-platform/core/blob/master/src/DataProvider/ItemDataProviderInterface.php) is used when fetching items. +Both implementations can also implement a third, optional interface called +['RestrictedDataProviderInterface'](https://github.com/api-platform/core/blob/master/src/DataProvider/ItemDataProviderInterface.php) +if you want to limit their effects to a single resource or operation. + In the following examples we will create custom data providers for an entity class called `AppBundle\Entity\BlogPost`. Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using `@ApiProperty(identifier=true)` for things to work properly (see also [Entity Identifier Case](serialization-groups-and-relations.md#entity-identifier-case)). @@ -35,19 +39,22 @@ If no data is available, you should return an empty array. namespace AppBundle\DataProvider; use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; +use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; use AppBundle\Entity\BlogPost; -final class BlogPostCollectionDataProvider implements CollectionDataProviderInterface +final class BlogPostCollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface { - public function getCollection(string $resourceClass, string $operationName = null) + public function supports(string $resourceClass, string $operationName = null): bool { - if (BlogPost::class !== $resourceClass) { - throw new ResourceClassNotSupportedException(); - } + return BlogPost::class === $resourceClass; + } + public function getCollection(string $resourceClass, string $operationName = null): \Generator + { // Retrieve the blog post collection from somewhere - return [new BlogPost(1), new BlogPost(2)]; + yield new BlogPost(1); + yield new BlogPost(2); } } ``` @@ -85,17 +92,20 @@ The `getItem` method can return `null` if no result has been found. namespace AppBundle\DataProvider; use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; +use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; use AppBundle\Entity\BlogPost; -final class BlogPostItemDataProvider implements ItemDataProviderInterface +final class BlogPostItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface { - public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) { - if (BlogPost::class !== $resourceClass) { - throw new ResourceClassNotSupportedException(); - } + public function supports(string $resourceClass, string $operationName = null): bool + { + return BlogPost::class === $resourceClass; + } - // Retrieve the blog post item from somewhere + public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?BlogPost + { + // Retrieve the blog post item from somewhere then return it or null if not found return new BlogPost($id); } }