Skip to content

thunderer/Serializard

Repository files navigation

Serializard

Build StatusLatest Stable VersionLicenseScrutinizer Code QualityCode CoverageDependency Status

Serializard is a library for (un)serialization of data of any complexity. Its main focus is to give user as much flexibility as possible by delegating the (un)serialization logic to the programmer to encourage good object design and only supervising the process hiding the unpleasant details about it.

Installation

This library is available on Composer/Packagist as thunderer/serializard.

Usage

Let's consider a simple User class with two properties and some setup code:

finalclass User{private$id; private$name; publicfunction__construct(int$id, string$name){/* ... */ } publicfunctiongetId(){return$this->id} publicfunctiongetName(){return$this->name} } $user = newUser(1, 'Thomas'); $formats = newFormatContainer(); $formats->add('json', newJsonFormat()); $hydrators = newFallbackHydratorContainer(); $normalizers = newFallbackNormalizerContainer(); $serializard = newSerializard($formats, $normalizers, $hydrators);

Serialization

Serialization is controlled by registering handlers used in normalization phase:

$normalizers->add(User::class, function(User$user){return [ 'id' => $user->getId(), 'name' => $user->getName(), ]}); $result = $serializard->serialize($user, 'json'); // result is{"id":1,"name":"Thomas"}

Unserialization

Unserialization can be controlled by registering callables able to reconstruct objects from data parsed from input text:

$hydrators->add(User::class, function(array$data){returnnewUser($data['id'], $data['name'])}); $json = '{"id":1,"name":"Thomas"}'; $user = $serializard->unserialize($json, User::class, 'json');

Formats

  • JSON in JsonFormat converts objects to JSON,
  • Array in ArrayFormat just returns object graph normalized to arrays of scalars,
  • YAML in YamlFormat converts objects to YAML (uses symfony/yaml),
  • XML in XmlFormat converts objects to XML (uses ext-dom).

License

See LICENSE file in the main directory of this library.

Packages

No packages published

Languages