matthiasnoback/broadway-serialization

Serialization helpers for Broadway

v2.1.0 2017-05-05 19:35 UTC

README

By Matthias Noback

Build Status Coverage Status

Event-sourcing framework Broadway requires a lot of your objects (like domain events and often view models as well) to be serializable because they have to be stored in plain text and later be reconstituted.

Making an object serializable requires you to write lots of pretty simple code. Even though that code is simple, you are likely to make mistakes in it. It's also a bit boring to write. To alleviate the pain, this library helps you to make objects serializable with in a few simple steps.

Installation

Just run

composer require matthiasnoback/broadway-serialization

Conventions

This library is very simple and it just works because of a few simple and yet assumptions:

  1. Serializable objects have at least one property. Properties can have any kind of scope.
  2. Serializable objects implements Broadway's Serializable interface.
  3. Properties contain scalar values, serializable objects, or arrays of serializable objects.

Example


use Broadway\Serializer\Serializable;
use BroadwaySerialization\Serialization\AutoSerializable;

class SerializableObject implements SerializableInterface
{
    use AutoSerializable;

    /**
     * @var string
     */
    private $foo;
    
    /**
     * @var SerializableObject
     */
    private $bar;
    
    /**
     * @var SerializableObject[]
     */
    private $bars = [];

    protected static function deserializationCallbacks()
    {
        return [
            'bar' => ['SerializableObject', 'deserialize'],
            'bars' => ['SerializableObject', 'deserialize']
        ];
    }
}

By implementing deserializationCallbacks() you can define callables that should be called in order to deserialize the provided data. The callable will be called once for single values or multiple times for arrays of values.

Setup

The Serializable trait depends on a little bit of setup. Before calling its deserialize() method, make sure you have properly set up a Reconstitute service, like this:

use BroadwaySerialization\Reconstitution\Reconstitution;
use BroadwaySerialization\Reconstitution\ReconstituteUsingInstantiatorAndHydrator;
use Doctrine\Instantiator\Instantiator;
use BroadwaySerialization\Hydration\HydrateUsingReflection;

Reconstitution::reconstituteUsing(
    new ReconstituteUsingInstantiatorAndHydrator(
        new Instantiator(),
        new HydrateUsingReflection()
    )
);

Note that this package ships with different implementations of the hydrator. Depending on your setup and preferences, you can also use the HydrateUsingClosure class. The former generally performs better than the HydrateUsingReflection.

Symfony

If you're using Symfony, this can be managed for you automatically. Just register the BroadwaySerialization\SymfonyIntegration\BroadwaySerializationBundle in your application kernel.

Performance

When using this library, your personal performance will increase significantly. Of course, runtime performance will be worse (not noticeably though, unless you're actually deserializing millions of objects).