8000 [Serializer] Introduce the concept of AttributeTransformer · Issue #15761 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Introduce the concept of AttributeTransformer #15761

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

Closed
dosten opened this issue Sep 11, 2015 · 4 comments
Closed

[Serializer] Introduce the concept of AttributeTransformer #15761

dosten opened this issue Sep 11, 2015 · 4 comments

Comments

@dosten
Copy link
Contributor
dosten commented Sep 11, 2015

Hi folks!

In a recent project I found that if some define a callback when normalize a object (i.e. Convert a DateTime object to a ISO8601 string) there isn't a easy way to convert the ISO8601 string into a DateTime object, so, I propose to introduce the concept of AttributeTransformers as a replacement of the concept of callbacks.
The idea is very similar to the concept of DataTransformers in the Form component.
We would have a interface that should be implemented by all transformers:

<?php

namespace Symfony\Component\Serializer;

interface AttributeTransformerInterface
{
    public function transform($value);
    public function reverseTransform($value);
}

The transform method will be called in the normalization (Similar to the current behavior).
The reverseTransform will be called in the denormalization.

This will require a new method AbstractNormalizer::setTransformer($attribute, AttributeTransformerInterface $transformer).

IMO this can be implemented in a BC way, the idea is deprecate the concept of callbacks in favor of the concept of attribute transformers.

@derrabus
Copy link
Member

What would be the difference between a normalizer and a transformer in this context?

@dosten
Copy link
Contributor Author
dosten commented Sep 14, 2015

The normalizer converts a whole object into an array, a transformer convert a single property of this object into any thing you want (i.e A DateTime object into a ISO8601 string). Think that a transformer is an extended version of the "callbacks" provided by normalizers (http://symfony.com/doc/current/components/serializer.html#using-callbacks-to-serialize-properties-with-object-instances) but the callbacks only works in the normalization, if you want to denormalize an array into an object, you can't.

@derrabus
Copy link
Member

I see. But if I had a normalizer that handles DateTime instances and a mechanism to indicate the type of a certain property (like described in #14844 for instance), would I still need those transformers?

@dunglas
Copy link
Member
dunglas commented Sep 21, 2015

We do it the same way as described by @derrabus in API Platform: https://github.com/dunglas/DunglasApiBundle/blob/master/JsonLd/Serializer/DateTimeNormalizer.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
0