Handles serialization and deserialization of data structures, in array and JSON structures.
composer require tiny-blocks/serializer
The library exposes available behaviors through the Serializer
interface, and the implementation of these behaviors
through the SerializerAdapter
trait.
<?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)
{
}
}
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"}
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
# )
Serializer is licensed under MIT.
Please follow the contributing guidelines to contribute to the project.