chubbyphp/chubbyphp-serialization

Chubbyphp Serialize

3.1.2 2021-10-03 10:54 UTC

README

CI Coverage Status Infection MSI Latest Stable Version Total Downloads Monthly Downloads

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A simple serialization.

Requirements

  • php: ^7.4|^8.0
  • doctrine/inflector: ^1.0|^2.0
  • psr/http-message: ^1.0.1
  • psr/link: ^1.0
  • psr/log: ^1.1.3

Suggest

  • chubbyphp/chubbyphp-container: ^1.1
  • pimple/pimple: ^3.3
  • psr/container: ^1.0
  • symfony/dependency-injection: ^4.4.34|^5.3.11|^6.0.0 (symfony integration)
  • symfony/yaml: ^4.4.34|^5.3.11|^6.0.0 (application/x-yaml support)

Installation

Through Composer as chubbyphp/chubbyphp-serialization.

composer require chubbyphp/chubbyphp-serialization "^3.1"

Usage

Accessor

Encoder

Type Encoder

Link

Normalizer

Field Normalizer

Relation Field Normalizer

Link Normalizer

Normalizer Context

NormalizerObjectMappingRegistry

Mapping

NormalizationFieldMapping

NormalizationLinkMapping

NormalizationObjectMapping

LazyNormalizationObjectMapping

Policy

ServiceFactory

chubbyphp-container

chubbyphp-laminas-config-factory

ServiceProvider

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/jsonx+xml',
//    'application/x-www-form-urlencoded',
//    'application/xml',
//    'application/x-yaml'
//]

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

Copyright

Dominik Zogg 2021