GithubHelp home page GithubHelp logo

renowncoder / serializard Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thunderer/serializard

0.0 0.0 0.0 81 KB

Flexible serializer encouraging good object design

Home Page: http://kowalczyk.cc

License: MIT License

PHP 99.96% Makefile 0.04%

serializard's Introduction

Serializard

Build Status Latest Stable Version License Scrutinizer Code Quality Code Coverage Dependency 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:

final class User
{
    private $id;
    private $name;

    public function __construct(int $id, string $name) { /* ... */ }

    public function getId() { return $this->id; }
    public function getName() { return $this->name; }
}

$user = new User(1, 'Thomas');

$formats = new FormatContainer();
$formats->add('json', new JsonFormat());

$hydrators = new FallbackHydratorContainer();
$normalizers = new FallbackNormalizerContainer();
$serializard = new Serializard($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) {
    return new User($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.

serializard's People

Contributors

thunderer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.