The Smooth PHP CQRS ES framework comes with a simple serializer, designed to get you started as quickly as possible. Usage of the serializer will allow you to quick convert complex DTO's to simple associative arrays.
<?phpnamespaceApp;
useSmoothPhp\Contracts\Serialization\Serializable;
class MyDTO implements Serializable {
publicfunction__construct(Id$id, DateTime$creationDate) {
$this->id = $id;
$this-creationDate = $creationDate;
}
// getterspublicfunctionserialize()
{
return [
'id' => (string) $this->id,
'date' => $this->creationDate->serialize(),
];
}
publicstaticfunctiondeserialize(array$data)
{
returnnewstatic(
newId($data['id']),
DateTime::deserialize($data['date'])
);
}
}
$dto = newApp\MyDTO(newApp\Id(uuid()), App\DateTime::now());
$serializer = new \SmoothPhp\Serialization\ObjectSelfSerializer;
// Store in database, job queue, etc$serialized = $serializer->serialize($dto);
// Use in app$object = $serializer->deserialize($serialized);