PHP code example of eureka / component-serializer

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

    

eureka / component-serializer example snippets



namespace Application;

use Application\VO\AnyObject;
use Eureka\Component\Serializer\JsonSerializer;

$serializer = new JsonSerializer();

$originalVO = new AnyObject(1, 'name', 'any arg');

//~ Serialize a VO into json string
$json = $serializer->serialize($originalVO);

//~ Unserialize a serialized VO
$unserializedVO = $serializer->unserialize($json, Application\VO\AnyObject::class);



declare(strict_types=1);

namespace Application\VO;

use Eureka\Component\Serializer\Exception\CollectionException;
use Eureka\Component\Serializer\VO\AbstractCollection;

class CollectionEntityB extends AbstractCollection implements \JsonSerializable
{
    /**
     * Class constructor.
     *
     * @param array
     */
    public function __construct(array $dataEntitiesB)
    {
        foreach ($dataEntitiesB as $dataEntityB) {
            $this->add(new EntityB($dataEntityB['id'], $dataEntityB['name']));
        }
    }

    /**
     * Override parent method to ensure we have only 



declare(strict_types=1);

namespace Application\VO;

use Eureka\Component\Serializer\JsonSerializableTrait;

class EntityA implements \JsonSerializable
{
    use JsonSerializableTrait;

    private int $id;
    private string $name;
    private ?CollectionEntityB $listEntitiesB;

    /**
     * EntityA constructor.
     *
     * @param int $id
     * @param string $name
     * @param CollectionEntityB|null $listEntitiesB
     */
    public function __construct(
        int $id,
        string $name,
        ?CollectionEntityB $listEntitiesB = null
    ) {
        $this->id            = $id;
        $this->name          = $name;
        $this->listEntitiesB = $listEntitiesB;
    }
    
    //...
}



declare(strict_types=1);

namespace Application\VO;

use Eureka\Component\Serializer\JsonSerializableTrait;

class EntityB implements \JsonSerializable
{
    use JsonSerializableTrait;

    private int $id;
    private string $name;

    public function __construct(
        int $id,
        string $name
    ) {
        $this->id   = $id;
        $this->name = $name;
    }
    
    //...
}


namespace Application;

use Application\VO\CollectionEntityB;
use Application\VO\EntityA;
use Application\VO\EntityB;
use Eureka\Component\Serializer\JsonSerializer;

$serializer = new JsonSerializer();

$dataList = [
    ['id' => 1, 'name B#1'],
    ['id' => 2, 'name B#2'],
];
$originalVO = new EntityA(1, 'name', new CollectionEntityB($dataList));

//~ Serialize a VO into json string
$json = $serializer->serialize($originalVO);

//~ Unserialize a serialized VO
$unserializedVO = $serializer->unserialize($json, Application\VO\EntityA::class);

//~ Manipulate collection from unserialized entity
foreach ($unserializedVO->getCollectionEntityB() as $entityB) {
    echo $entityB->getName() . PHP_EOL;
}
bash
make php-min-compatibility
bash
make php-max-compatibility