Skip to content

tiny-blocks/serializer

Repository files navigation

Serializer

License

Overview

Handles serialization and deserialization of data structures, in array and JSON structures.

Installation

composer require tiny-blocks/serializer

How to use

The library exposes available behaviors through the Serializer interface, and the implementation of these behaviors through the SerializerAdapter trait.

Concrete implementation

<?php

namespace Example;

use TinyBlocks\Serializer\Serializer;
use TinyBlocks\Serializer\SerializerAdapter;

final readonly class Amount implements Serializer
{
    use SerializerAdapter;

    public function __construct(private float $value, private string $currency)
    {
    }
}

Using the toJson method

The toJson method returns the representation of the object in JSON format.

$amount = new Amount(value: 1.25, currency: 'USD');

$amount->toJson(); # {"value":1.25,"currency":"USD"}

Using the toArray method

The toArray method returns the representation of the object in array format.

$amount = new Amount(value: 1.25, currency: 'USD');

$amount->toArray(); # Array
                    # (
                    #     [value] => 1.25
                    #     [currency] => USD
                    # )

License

Serializer is licensed under MIT.

Contributing

Please follow the contributing guidelines to contribute to the project.