cnastasi / serializer
A experimental serializer using php 7.4 typed properties and reflection
Installs: 10 764
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.4
- cnastasi/ddd: ^0.2
Requires (Dev)
- phpunit/phpunit: ^9.3
- vimeo/psalm: ^4.6
README
Serializer
It permits to serialize and unserialize value objects.
- Simple Objects: With just a primitive inside
- Composite Objects: Objects formed by composition of two or more Simple Objects
- Collections: Objects formed by a collection of Simple or Composite objects
How to install
How to use
Serialize a simple value object
use CNastasi\Serializer\Serializer\SimpleValueObjectSerializer; use CNastasi\Example\Age; $serializer = new SimpleValueObjectSerializer(); // $result === 37 $result = $serializer->serialize(new Age(37));
Unserialize a simple value object
use CNastasi\Serializer\Unserializer\SimpleValueObjectUnserializer; use CNastasi\Example\Age; $serializer = new SimpleValueObjectUnserializer(); // $result === Age(37) $result = $serializer->unserialize(37, Age::class);
Complex example
require_once 'vendor/autoload.php'; use CNastasi\Example\Address; use CNastasi\Example\Age; use CNastasi\Example\Name; use CNastasi\Example\Person; use CNastasi\Example\Phone; use CNastasi\Serializer\Serializer\CompositeValueObjectSerializer; use CNastasi\Serializer\Serializer\SimpleValueObjectSerializer; use CNastasi\Serializer\DefaultSerializer; // Initialize the Serializer $simpleValueObjectSerializer = new SimpleValueObjectSerializer(); $compositeValueObjectSerializer = new CompositeValueObjectSerializer($simpleValueObjectSerializer); $serializer = new DefaultSerializer([$simpleValueObjectSerializer, $compositeValueObjectSerializer]); // Create your domain objects $age = new Age(37); // A simple one $address = new Address('145 Main Street', 'New York'); // A composite // A composite and complex, with nullable and recursion $person = new Person( new Name ('Christian Nastasi'), new Age(37), new Address('42 Somewhere Street', 'World'), new Phone('+39 123456754') ); // Serialize the objects var_dump($serializer->serialize($age)); /* * int(37) */ var_dump($serializer->serialize($address)); /* * array(2) { * 'street' => string(15) "145 Main Street" * 'city' => string(8) "New York" * } */ var_dump($serializer->serialize($person)); /* array(5) { 'name' => string(17) "Christian Nastasi" 'age' => int(37) 'address' => array(2) { 'street' => string(19) "42 Somewhere Street" 'city' => string(5) "World" } 'phone' => string(13) "+39 123456754" 'parent' => array(5) { 'name' => string(17) "Christian Nastasi" 'age' => int(37) 'address' => array(2) { 'street' => string(19) "42 Somewhere Street" 'city' => string(5) "World" } 'phone' => string(13) "+39 123456754" 'parent' => NULL } } */