tiny-blocks/serializer

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

3.3.2 2024-10-08 14:50 UTC

This package is auto-updated.

Last update: 2024-11-08 15:07:07 UTC


README

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.