JMS Serializer. Create two ways to serialize a one-to-many model

I am using JMS Serializer for a PHP project and stumbled upon one problem.

Look at the code

<?php
use JMS\Serializer\Annotation as Serializer;

/**
 * @Serializer\ExclusionPolicy("all")
 */
class Order
{
    /**
     * @var int
     * @Serializer\Type("integer")
     * @Serializer\Expose
     */
    private $id;

    /**
     * @var Product[]
     * @Serializer\Type("array<Product>")
     * @Serializer\Expose
     */
    private $products;

    /**
     * @var float
     * @Serializer\Type("float")
     * @Serializer\Expose
     */
    private $total;

    private $someInternalProperty;

    function __construct($products)
    {
        $this->id = rand(0, 100);
        $this->products = $products;
        $this->total = rand(100, 1000);
        $this->someInternalProperty = 'Flag';
    }
}

/**
 * @Serializer\ExclusionPolicy("all")
 */
class Product
{
    /**
     * @var int
     * @Serializer\Type("integer")
     * @Serializer\Expose
     */
    private $id;

    /**
     * @var string
     * @Serializer\Type("string")
     * @Serializer\Expose
     */
    private $name;

    private $price;

    private $description;

    function __construct($id, $name, $price, $description)
    {
        $this->id = $id;
        $this->name = $name;
        $this->price = $price;
        $this->description = $description;
    }
}

$order = new Order([
    new Product(
        1,
        'Banana',
        10,
        'Yellow'
    ),
    new Product(
        2,
        'Tomato',
        12,
        'Red'
    )
]);

$serializer = \JMS\Serializer\SerializerBuilder::create()
    ->setPropertyNamingStrategy(new \JMS\Serializer\Naming\SerializedNameAnnotationStrategy(new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy()))
    ->build();

print_r(
    $serializer->serialize(
        $order,
        'json',
        \JMS\Serializer\SerializationContext::create()
            ->setSerializeNull(true)
    )
);

Here I showed a simplified example of my code. I use it to store history of order changes. Before and after the upgrade, I save this serialized model to the database. Good.

clientide. , , - . $id $name "Group ({'history', 'edit'})" "Group ({'edit'})". , , . " " , ​​ $price $description.

, , (, )? - ( , ).

+4
1

. , , " ".

<?php

require_once __DIR__ . '/../../../app/Autoload.php';

use JMS\Serializer\Annotation as Serializer;

/**
 * @Serializer\ExclusionPolicy("all")
 */
class Order
{
    /**
     * @var int
     * @Serializer\Type("integer")
     * @Serializer\Expose
     */
    private $id;

    /**
     * @var Product[]
     * @Serializer\Type("array<Product>")
     * @Serializer\Expose
     */
    private $products;

    /**
     * @var float
     * @Serializer\Type("float")
     * @Serializer\Expose
     */
    private $total;

    private $someInternalProperty;

    function __construct($products)
    {
        $this->id = rand(0, 100);
        $this->products = $products;
        $this->total = rand(100, 1000);
        $this->someInternalProperty = 'Flag';
    }
}

/**
 * @Serializer\ExclusionPolicy("all")
 */
class Product
{
    /**
     * @var int
     * @Serializer\Expose
     * @Serializer\Type("integer")
     * @Serializer\Groups({"Default", "edit"})
     */
    private $id;

    /**
     * @var string
     * @Serializer\Expose
     * @Serializer\Type("string")
     * @Serializer\Groups({"Default", "edit"})
     */
    private $name;

    /**
     * @Serializer\Expose
     * @Serializer\Groups({"edit"})
     */
    private $price;

    /**
     * @Serializer\Expose
     * @Serializer\Groups({"edit"})
     */
    private $description;

    private $hiddenProperty;

    function __construct($id, $name, $price, $description)
    {
        $this->id = $id;
        $this->name = $name;
        $this->price = $price;
        $this->description = $description;
        $this->hiddenProperty = 42;
    }
}

$product1 = new Product(
    1,
    'Banana',
    10,
    'Yellow'
);

$order = new Order([
    $product1,
    new Product(
        2,
        'Tomato',
        12,
        'Red'
    )
]);

$serializer = \JMS\Serializer\SerializerBuilder::create()
    ->setPropertyNamingStrategy(new \JMS\Serializer\Naming\SerializedNameAnnotationStrategy(new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy()))
    ->build();

print_r([
    $serializer->serialize(
        $order,
        'json',
        \JMS\Serializer\SerializationContext::create()
            ->setSerializeNull(true)
            ->setGroups(['Default'])
    ),
    $serializer->serialize(
        $product1,
        'json',
        \JMS\Serializer\SerializationContext::create()
            ->setSerializeNull(true)
            ->setGroups(['edit'])
    ),

]);

:

Array
(
    [0] => {"id":86,"products":[{"id":1,"name":"Banana"},{"id":2,"name":"Tomato"}],"total":644} // Here I have short model for history
    [1] => {"id":1,"name":"Banana","price":10,"description":"Yellow"} // And here I have expanded model for other purpose.
)

JMS-Serializer:)

+4

Source: https://habr.com/ru/post/1529781/


All Articles