Skip to content

Add documentation about SerializerAwareDataProviderInterface #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions core/data-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,45 @@ services:
tags: [ 'api_platform.item_data_provider' ]
```

## Injecting the Serializer in an `ItemDataProvider`

In some cases, you may need to inject the `Serializer` in your `DataProvider`. There are no issues with the
`CollectionDataProvider`, but when injecting it in the `ItemDataProvider` it will throw a `CircularReferenceException`.

For this reason, we implemented the `SerializerAwareDataProviderInterface`:

```php
<?php
// src/AppBundle/DataProvider/BlogPostItemDataProvider.php

namespace AppBundle\DataProvider;

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\SerializerAwareDataProviderInterface;
use ApiPlatform\Core\DataProvider\SerializerAwareDataProviderTrait;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use AppBundle\Entity\BlogPost;

final class BlogPostItemDataProvider implements ItemDataProviderInterface, SerializerAwareDataProviderInterface
{
use SerializerAwareDataProviderTrait;

public function supports(string $resourceClass, string $operationName = null): bool
{
return BlogPost::class === $resourceClass;
}

public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?BlogPost
{
// Retrieve data from anywhere you want, in a custom format
$data = '...';

// Deserialize data using the Serializer
return $this->getSerializer()->deserialize($data, BlogPost::class, 'custom');
}
}
```

Previous chapter: [Extending JSON-LD context](extending-jsonld-context.md)

Next chapter: [Extensions](extensions.md)
5 changes: 3 additions & 2 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@
12. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
13. [Extending JSON-LD context](core/extending-jsonld-context.md)
14. [Data Providers](core/data-providers.md)
1. [Custom Collection Data Provider](core/data-providers.md#creating-a-custom-data-provider#custom-collection-data-provider)
2. [Custom Item Data Provider](core/data-providers.md#returning-a-paged-collection#custom-item-data-provider)
1. [Custom Collection Data Provider](core/data-providers.md#custom-collection-data-provider)
2. [Custom Item Data Provider](core/data-providers.md#custom-item-data-provider)
3. [Injecting the Serializer in an `ItemDataProvider`](core/data-providers.md#injecting-the-serializer-in-an-itemdataprovider)
15. [Extensions](core/extensions.md)
1. [Custom Extension](core/extensions.md#custom-extension)
2. [Filter upon the current user](core/extensions.md#example)
Expand Down