Symfony Serializer


Un composant méconnu

La serialisation

JMS Serializer

Reprendre la main

  • Cohérent
  • Testé
  • Performant

Symfony Serializer

Encodage

Normalization

💰 Valeur métier 💰

Coding time!

Just do it


public function normalize($object, $format = null, array $context = array())
{
    return [
        'id'     => $object->getId(),
        'name'   => $object->getName(),
        'groups' => array_map(
            function (Group $group) {
                return $group->getId();
            },
            $object->getGroups()
        )
    ];
}
                    

Implemente NormalizerInterface

class UserNormalizer implements NormalizerInterface {}

Affiches ton soutien !


public function supportsNormalization($data, $format = null)
{
    return $data instanceof User;
}
                    

Résultat


[
    'id'     => 1,
    'name'   => 'Foo Bar',
    'groups' => [1, 2]
]
                    

Associations

Délegue


public function normalize($object, $format = null, array $context = array())
{
    return [
        // ...
        'groups' => array_map(
            function ($object) use ($format, $context) {
                return $this->serializer->normalize($object, $format, $context);
            },
            $object->getGroups()
        ),
    ];
}
                    

Étends SerializerAwareNormalizer

class UserNormalizer extends SerializerAwareNormalizer implements NormalizerInterface {}

Complète ton serializer


class GroupNormalizer extends SerializerAwareNormalizer implements NormalizerInterface
{
    public function normalize($object, $format = null, array $context = array())
    {
        return [
            'id'   => $object->getId(),
            'name' => $object->getName(),
        ];
    }

    public function supportsNormalization($data, $format = null)
    {
        return $data instanceof Group;
    }
}
                    

Résultat:


[
    'id'        => 1,
    'firstname' => 'Foo',
    'lastname'  => 'Bar',
    'groups'    => [
        [
            'id'   => 1,
            'name' => 'FooFighters'
        ],
        [
            'id'   => 2,
            'name' => 'BarFighters'
        ],
    ],
]
                        

Composition

Serializer à la carte 👌


services:
    # Serializer
    acme.serializer.default:
        class: 'Symfony\Component\Serializer\Serializer'
        arguments:
            0:
                - '@acme.normalizer.user'
                - '@acme.normalizer.group'
                - '@serializer.normalizer.object'
            1:
                - '@acme.encoder.json'
                - '@acme.encoder.xml'
                        

Encoders:


services:
    # JSON Encoder
    acme.encoder.json:
        class: 'Symfony\Component\Serializer\Encoder\JsonEncoder'

    # XML Encoder
    acme.encoder.xml:
        class: 'Symfony\Component\Serializer\Encoder\XmlEncoder'
                        

Normalizers:


services:
    # User Normalizer
    acme.normalizer.user:
        class: 'Acme\Serializer\Normalizer\UserNormalizer'

    # Group Normalizer
    acme.normalizer.group:
        class: 'Acme\Serializer\Normalizer\GroupNormalizer'
                        

Quelques exemples

  • DateTime
  • Collection
  • Form Error
  • Entité

Avantages

  • 👍 Cohérent
  • 👍 Testable
  • 👍 Flexible
  • 👍 Separation of concern
  • 👍 Plusieurs serlializer

Merci !