PHP code example of fdevs / serializer

1. Go to this page and download the library: Download fdevs/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/ */

    

fdevs / serializer example snippets




namespace App\Model;

class Post
{
    /**
     * @var string
     */
    protected $id;

    /**
     * @var \DateTime
     */
    protected $createdAt;

    /**
     * @var int
     */
    protected $views;

    /**
     * @var bool
     */
    protected $show;

    /**
     * @var User
     */
    protected $user;

    /**
     * @var float
     */
    protected $rate;

    /**
     * @var array|Comment[]
     */
    protected $comments;

    //init geters and setters
}



use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use FDevs\Serializer\Normalizer\ObjectNormalizer as FDevsNormalizer;
use FDevs\Serializer\Mapping\Factory\LazyLoadingMetadataFactory;
use FDevs\Serializer\Mapping\Loader\XmlFilesLoader;
use App\Model\Post;

$files = ['/path/to/xml/Model.Post.xml'];
$loader = new XmlFilesLoader($files);

//init metadata factory
$loadingMetadata = new LazyLoadingMetadataFactory($loader);


$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new FDevsNormalizer($loadingMetadata), new ObjectNormalizer());

$serializer = new Serializer($normalizers, $encoders);

//init $post
$post = new Post();
echo $serializer->serialize($post);

//get your array data
$data = [];
$post = $serializer->serialize($data,Post::class);



namespace App\Serializer\Option\NameConverter;

use FDevs\Serializer\Option\NameConverterInterface;

class NameSuffix implements NameConverterInterface
{
    /**
     * {@inheritdoc}
     */
    public function convert($propertyName, array $options, $type = self::TYPE_NORMALIZE)
    {
        return $propertyName.implode('', $options);
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'name-suffix';
    }
}



namespace App\Serializer\Option\Visible;

use FDevs\Serializer\Option\VisibleInterface;

class VersionOption implements VisibleInterface
{
    const CONTEXT_KEY = 'version';

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'version';
    }

    /**
     * {@inheritdoc}
     */
    public function isVisible($propertyName, $value, array $options, array $context)
    {
        return !isset($context[static::CONTEXT_KEY]) || !is_array($context[static::CONTEXT_KEY]) || !count($context[static::CONTEXT_KEY]) || $context[static::CONTEXT_KEY] == reset($options);
    }
}

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use FDevs\Serializer\Normalizer\ObjectNormalizer as FDevsNormalizer;
use FDevs\Serializer\Mapping\Factory\LazyLoadingMetadataFactory;
use FDevs\Serializer\Mapping\Loader\XmlFilesLoader;
use FDevs\Serializer\OptionRegistry;
use App\Serializer\Option\NameConverter\NameSuffix;
use App\Serializer\Option\Visible\VersionOption;
use FDevs\Serializer\DataTypeFactory;
use App\Model\Post;

$files = ['/path/to/xml/Model.Post.xml'];
$loader = new XmlFilesLoader($files);

//init metadata factory
$loadingMetadata = new LazyLoadingMetadataFactory($loader);

//create OptionRegistry
$optionRegistry = new OptionRegistry();
$optionRegistry->addOption(new NameSuffix());
$optionRegistry->addOption(new VersionOption());

//create DataTypeFactory
$dataTypeFactory = new DataTypeFactory();

$normalizer = new FDevsNormalizer($loadingMetadata, $dataTypeFactory, $optionRegistry);


$serializer = new Serializer([$normalizer, new ObjectNormalizer()], [new XmlEncoder(), new JsonEncoder()]);