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

Conversation

vincentchalamon
Copy link
Contributor

## Inject Serializer in a ItemDataProvider

In some case, you may need to inject the Serializer in your DataProvider. For CollectionDataProvider, you can inject it
easily without risk, but for ItemDataProvider you will experience a CircularReferenceException.
Copy link
Member

@soyuka soyuka Nov 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

In some case, you may need to inject the Serializer in your DataProvider. For CollectionDataProvider, you can inject it
easily without risk, but for ItemDataProvider you will experience a CircularReferenceException.

That's why we implemented the SerializerAwareDataProviderInterface to help you inject the Serializer dependency:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this reason, we implemented the SerializerAwareDataProviderInterface:

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this stuff. Nice improvement!

@@ -125,6 +125,46 @@ services:
tags: [ 'api_platform.item_data_provider' ]
```

## Inject Serializer in a ItemDataProvider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Injecting the Serializer in an `ItemDataProvider`

@@ -125,6 +125,46 @@ services:
tags: [ 'api_platform.item_data_provider' ]
```

## Inject Serializer in a ItemDataProvider

In some cases, you may need to inject the Serializer in your DataProvider. There are no issues with the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add backticks aroung class and exception names (everywhere)?


```php
<?php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line to remove

// Retrieve data from anywhere you want, in a custom format
$data = '...';

// Deserialize data using Serializer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Serializer

@dunglas
Copy link
Member

dunglas commented Nov 9, 2017

Can you also update the TOC please?

@dunglas
Copy link
Member

dunglas commented Nov 9, 2017

Thanks @vincentchalamon!

@dunglas dunglas merged commit 72405e8 into api-platform:master Nov 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants