PHP code example of liip / serializer

1. Go to this page and download the library: Download liip/serializer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liip / serializer example snippets


use Doctrine\Common\Annotations\AnnotationReader;
use Liip\MetadataParser\Builder;
use Liip\MetadataParser\Parser;
use Liip\MetadataParser\RecursionChecker;
use Liip\MetadataParser\ModelParser\JMSParser;
use Liip\MetadataParser\ModelParser\LiipMetadataAttributeParser;
use Liip\MetadataParser\ModelParser\PhpDocParser;
use Liip\MetadataParser\ModelParser\ReflectionParser;
use Liip\Serializer\DeserializerGenerator;
use Liip\Serializer\Serializer;
use Liip\Serializer\SerializerGenerator;
use Liip\Serializer\Template\Deserialization;
use Liip\Serializer\Template\Serialization;

$configuration = GeneratorConfiguration::createFomArray([
    'options' => [
        'allow_generic_arrays' => false,
    ],
    'default_group_combinations' => ['api'],
    'default_versions' => ['', '1', '2'],
    'classes' => [
         Product::class => [
             'default_versions' => ['1', '2'], // optional, falls back to global list
             'group_combinations' => [ // optional, falls back to global default_group_combinations
                 [
                     'groups' => [], // generate without groups
                 ],
                 [
                     'groups' => ['api'], // global groups are overwritten, not merged. versions are taken from class default
                 ],
                 [
                     'groups' => ['api', 'detail'],
                     'versions' => ['2'], // only generate the combination of api and detail for version 2
                 ],
             ],
         ],
         Other::class => [], // generate this class with default groups and versions
    ]
]);

$parsers = [
    new ReflectionParser(),
    new PhpDocParser(),
    new JMSParser(new AnnotationReader()),
    new LiipMetadataAttributeParser(),
];
$builder = new Builder(new Parser($parsers), new RecursionChecker(null, []));

$serializerGenerator = new SerializerGenerator( new Serialization(), $configuration, $cacheDirectory);
$deserializerGenerator = new DeserializerGenerator(new Deserialization(), [Product::class, User::class], $cacheDirectory);
$serializerGenerator->generate($builder);
$deserializerGenerator->generate($builder);

use Liip\Serializer\DeserializerHandlerInterface;
use Liip\Serializer\SerializerHandlerInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
use Symfony\Component\Uid\UuidV3;
use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Uid\UuidV5;
use Symfony\Component\Uid\UuidV6;
use Symfony\Component\Uid\UuidV7;
use Symfony\Component\Uid\UuidV8;

/**
 * We write a single handler for both serialization and deserialization.
 */
class UuidHandler implements SerializerHandlerInterface, DeserializerHandlerInterface
{
    private const SUPPORTED_CLASSES = [
        Uuid::class,
        UuidV1::class,
        UuidV3::class,
        UuidV4::class,
        UuidV5::class,
        UuidV6::class,
        UuidV7::class,
        UuidV8::class,
    ];

    public function generateSerializeExpression(string $className, string $modelPath): string
    {
        // $modelPath is a PHP expression for the property value, e.g. "$model->id"
        return $modelPath.'->toString()';
    }

    public function generateDeserializeExpression(string $className, string $arrayPath): string
    {
        // $arrayPath is a PHP expression for the raw array value, e.g. "$array['id']"
        return '\\'.$className.'::fromString('.$arrayPath.')';
    }

    public function getSerializationClasses(string $className): array
    {
        return self::SUPPORTED_CLASSES;
    }

    public function getDeserializationClasses(string $className): array
    {
        return self::SUPPORTED_CLASSES;
    }
}

$configuration = GeneratorConfiguration::createFromArray([
    'options' => [
        'handlers' => [new UuidHandler()],
    ],
    // ...
]);

use Acme\Model\Product;
use Liip\Serializer\Context;
use Liip\Serializer\Serializer;

$serializer = new Serializer($cacheDirectory);

// A model to serialize
$productModel = new Product();

// Your serialized data
$data = $serializer->serialize($productModel, 'json', (new Context())->setVersion(2));

use Acme\Model\Product;
use Liip\Serializer\Serializer;

$serializer = new Serializer($cacheDirectory);

// Data to deserialize
$data = '{
    "api_string": "api",
    "detail_string": "details",
    "nested_field": {
        "nested_string": "nested"
    },
    "date": "2018-08-03T00:00:00+02:00",
    "date_immutable": "2016-06-01T00:00:00+02:00"
}';

/** @var Product $model */
$model = $serializer->deserialize($data, Product::class, 'json');