Skip to content

chubbyphp/chubbyphp-deserialization

Repository files navigation

chubbyphp-deserialization

CI Coverage Status Mutation testing badge 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 deserialization.

Requirements

  • php: ^8.1
  • chubbyphp/chubbyphp-decode-encode: ^1.1
  • psr/http-message: ^1.1|^2.0
  • psr/log: ^2.0|^3.0

Suggest

  • chubbyphp/chubbyphp-container: ^2.2
  • pimple/pimple: ^3.5
  • psr/container: ^2.0.2
  • symfony/config: ^5.4.31|^6.3.8|^7.0 (symfony integration)
  • symfony/dependency-injection: ^5.4.31|^6.3.8|^7.0 (symfony integration)

Installation

Through Composer as chubbyphp/chubbyphp-deserialization.

composer require chubbyphp/chubbyphp-deserialization "^4.1"

Usage

Accessor

Denormalizer

Field Denormalizer

Relation Field Denormalizer

Denormalizer Context

DenormalizerObjectMappingRegistry

Deserializer

<?php

use Chubbyphp\DecodeEncode\Decoder\Decoder;
use Chubbyphp\DecodeEncode\Decoder\JsonTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\JsonxTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\UrlEncodedTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\XmlTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\YamlTypeDecoder;
use Chubbyphp\Deserialization\Denormalizer\Denormalizer;
use Chubbyphp\Deserialization\Denormalizer\DenormalizerObjectMappingRegistry;
use Chubbyphp\Deserialization\Deserializer;
use MyProject\Deserialization\ModelMapping;
use MyProject\Model\Model;

$logger...;

$deserializer = new Deserializer(
    new Decoder([
        new JsonTypeDecoder(),
        new JsonxTypeDecoder(),
        new UrlEncodedTypeDecoder(),
        new XmlTypeDecoder(),
        new YamlTypeDecoder()
    ]),
    new Denormalizer(
        new DenormalizerObjectMappingRegistry([
            new ModelMapping()
        ]),
        $logger
    )
);

$model = $deserializer->deserialize(
    Model::class,
    '{"name": "php"}',
    'application/json'
);

echo $model->getName();
// 'php'

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

print_r($deserializer->decode(
    '{"name": "php"}',
    'application/json'
));
// ['name' => 'php']

$model = $denormalizer->denormalize(
    Model::class,
    ['name' => 'php']
);

echo $model->getName();
// 'php'

Mapping

DenormalizationFieldMapping

DenormalizationObjectMapping

LazyDenormalizationObjectMapping

Policy

ServiceFactory

chubbyphp-container

chubbyphp-laminas-config-factory

ServiceProvider

Copyright

2024 Dominik Zogg