chubbyphp/chubbyphp-serialization

Chubbyphp Serialize


README

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Monthly Downloads Latest Stable Version Latest Unstable Version

Description

A simple serialization.

Requirements

  • php: ~7.0
  • doctrine/inflector: ~1.0
  • psr/http-message: ~1.0
  • psr/link: ~1.0
  • psr/log: ~1.0

Suggest

  • container-interop/container-interop: ~1.0
  • pimple/pimple: ~3.0
  • symfony/dependency-injection: ~2.8|~3.0|~4.0 (symfony integration)
  • symfony/yaml: ~2.8|~3.0|~4.0 (application/x-yaml support)

Installation

Through Composer as chubbyphp/chubbyphp-serialization.

composer require chubbyphp/chubbyphp-serialization "~2.10"

Usage

Accessor

Encoder

Type Encoder

Link

Normalizer

Field Normalizer

Relation Field Normalizer

Link Normalizer

Normalizer Context

NormalizerObjectMappingRegistry

Mapping

NormalizationFieldMapping

NormalizationLinkMapping

NormalizationObjectMapping

LazyNormalizationObjectMapping

Provider

Serializer

<?php

use Chubbyphp\Serialization\Encoder\Encoder;
use Chubbyphp\Serialization\Encoder\JsonTypeEncoder;
use Chubbyphp\Serialization\Encoder\JsonxTypeEncoder;
use Chubbyphp\Serialization\Encoder\UrlEncodedTypeEncoder;
use Chubbyphp\Serialization\Encoder\XmlTypeEncoder;
use Chubbyphp\Serialization\Encoder\YamlTypeEncoder;
use Chubbyphp\Serialization\Normalizer\Normalizer;
use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistry;
use Chubbyphp\Serialization\Serializer;
use MyProject\Serialization\ModelMapping;
use MyProject\Model\Model;
use Psr\Http\Message\ServerRequestInterface;

$logger = ...;

$serializer = new Serializer(
    new Normalizer(
        new NormalizerObjectMappingRegistry([
            new ModelMapping()
        ]),
        $logger
    ),
    new Encoder([
        new JsonTypeEncoder(),
        new JsonxTypeEncoder(),
        new UrlEncodedTypeEncoder(),
        new XmlTypeEncoder(),
        new YamlTypeEncoder()
    ])
);

$model = new Model;
$model->setName('php');

$json = $serializer->serialize(
    $model,
    'application/json'
);

echo $json;
// '{"name": "php"}'

$model = new Model;
$model->setName('php');

$data = $serializer->normalize(
    $model
);

print_r($data);
// ['name' => 'php']

print_r($serializer->getContentTypes());
//[
//    'application/json',
//    'application/x-jsonx',
//    'application/x-www-form-urlencoded',
//    'application/xml',
//    'application/x-yaml'
//]

echo $serializer->encode(
    ['name' => 'php'],
    'application/json'
);
// '{"name": "php"}'

Copyright

Dominik Zogg 2017