zf-fr/ZfrRest — pull request #11 by bakura10 (2013-01-10 20:56:49+00:00)
Title: Initial commit of Serializer

Okey, so this is mainly inspired by active_model_serializers from RoR.

I have currently two interfaces:

## SerializerInterface

https://github.com/bakura10/ZfrRest/blob/serializer/src/ZfrRest/Serializer/SerializerInterface.php

This interface is simple, it just has a serialize method and a setter/getter for ClassMetadata.

## AssociationSerializerInterface

https://github.com/bakura10/ZfrRest/blob/serializer/src/ZfrRest/Serializer/Association/AssociationSerializerInterface.php

This one extends SerializerInterface, and it adds a way to set an item serializer. Basically, this is the serializer that will be used to serialize the *underlying* object of an association. This allows to easily change the item serializer (for isntance if we want to serialize less data for a given association, we can provide a ShortSomethingSerializer for a given association, for instance).

It also has a setEmbed that allow to specify how the association is serialized: either with identifiers (in which case it will return a simple identifier or an array of identifiers if this is a collection valued association - we may create another interface for those cases).

## ArraySerializerInterface

I've not done it, but I think to add an ArraySerializerInterface that would extend from SerializerInterface, and that would be used to serialize Collection (or anything that implements Traversable), where we would like to output some additional informations (count, num_page...). Of course we would provide out-of-the box implementation for some like Zend\Paginator.

## Concrete implementations

I have done a quick implementation of Serializer: https://github.com/bakura10/ZfrRest/blob/serializer/src/ZfrRest/Serializer/Serializer.php

This has ignoredAttributes and associationsSerializers.

Basically, if the idea is to be able to extend any Serializer when we want to output an entity differently. Of coruse, it could be cool if we could create Serializer through a factory AND through annotation.

## HATEOAS

With this architecture I think it's quite simple to provide HATEOAS. We just need to have specific HATEOAS that extend from Serializer and AssociationSerializer, and add a dependecy to the router to add some more attributes.