Skip to content

Latest commit

 

History

History
44 lines (37 loc) · 963 Bytes

simple.md

File metadata and controls

44 lines (37 loc) · 963 Bytes

Simple payload serializer

If you use the Payload package, you can simplify the serialization of your specific events.

use GpsLab\Domain\Event\Event;
use GpsLab\Domain\Event\Queue\Serializer\Serializer;

class JsonPayloadSerializer implements Serializer
{
    /**
     * @param object $data
     *
     * @return string
     */
    public function serialize($data)
    {
        return json_encode([
            'class' => get_class($data),
            'payload' => $object->payload(),
        ]);
    }

    /**
     * @param string $data
     *
     * @return object
     */
    public function deserialize($data)
    {
        $data = json_decode($data);

        if (empty($data['class']) || !class_exists($data['class'])) {
            throw new \InvalidArgumentException();
        }

        $class_name = $data['class'];

        return new $class_name($data['payload']);
    }
}