qlimix/serializable

An interface that indicates the object is serializable

2.0.0 2020-04-19 12:45 UTC

This package is auto-updated.

Last update: 2024-04-19 21:45:07 UTC


README

Travis CI Packagist MIT License

An interface that indicates the object is serializable.

Install

Using Composer:

$ composer require qlimix/serializable

usage

<?php

use Qlimix\Serializable\SerializableInterface;

final class FooBar implements SerializableInterface
{
    /** @var string */
    private $foo;
    
    /** @var int */
    private $bar;
    
    public function __construct(string $foo, int $bar)
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }
    
    public function getName(): string
    {
        return 'foo.bar';
    }
    
    public function serialize(): array
    {
        return [
            'foo' => $this->foo,
            'bar' => $this->bar,
        ];
    }

    public static function deserialize(array $data): SerializableInterface
    {
        return new self($data['foo'], $data['bar']);
    }
}

If whatever you are serializing doesn't cross the boundaries of your application you could use the GetClassNameTrait to refer to the object class name.

Testing

To run all unit tests locally with PHPUnit:

$ vendor/bin/phpunit

Quality

To ensure code quality run grumphp which will run all tools:

$ vendor/bin/grumphp run

Contributing

Please see CONTRIBUTING for details.