tiny-blocks / serializer
Handles serialization and deserialization of data structures, in array and JSON structures.
Installs: 23 177
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^8.3
Requires (Dev)
- infection/infection: ^0
- phpmd/phpmd: ^2.15
- phpstan/phpstan: ^1
- phpunit/phpunit: ^11
- squizlabs/php_codesniffer: ^3.11
README
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.