eureka/component-serializer

客户端用于序列化缓存中存储的响应 VO 的序列化器。

2.1.0 2024-08-22 15:51 UTC

This package is auto-updated.

Last update: 2024-09-22 15:56:35 UTC


README

Current version Supported PHP version CI Quality Gate Status Coverage

为什么?

用于序列化和反序列化对象的组件,主要用于客户端 SDK 对 VO(值对象)进行(反)序列化以进行缓存。

安装

如果您想在项目中安装它,请通过 composer 需求它

composer require eureka/component-serializer

用法

简单(反)序列化

此库将提供(反)序列化服务,以便安全地缓存值对象。

<?php
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);

为了正确地进行序列化和反序列化,VO 必须实现 \JsonSerializable 接口并使用提供的 JsonSerializableTrait。该特性将自动处理子 VO 的(反)序列化。

对于子 VO(集合)的列表,必须在构造函数中提供一个集合对象。

使用集合的复杂(反)序列化

首先,您需要一个集合对象。我们提供了一个 AbstractCollection 类来管理所有基本操作。

因此,创建一个 Collection 类

<?php

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 required sub VO entity. 
     * 
     * @param mixed $offset
     * @param mixed $value
     * @return void
     * @throws CollectionException
     */
    public function offsetSet($offset, $value)
    {
        if (!$value instanceof EntityB) {
            throw new CollectionException('Data must be an instance of ' . EntityB::class);
        }

        parent::offsetSet($offset, $value);
    }
}
<?php

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;
    }
    
    //...
}
<?php

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;
    }
    
    //...
}

现在,尝试对 VO 进行(反)序列化

<?php
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;
}

贡献

查看 CONTRIBUTING 文件。

安装/更新项目

您可以使用以下命令安装项目

make install

并且可以使用以下命令更新

make update

注意:对于组件,不提交 composer.lock 文件。

测试 & CI(持续集成)

测试

您可以使用以下命令在您的端运行测试(带有覆盖率)

make tests

您可以使用以下命令在您的端运行测试(带有覆盖率)

make integration

对于更清晰的输出(但没有覆盖率),您可以使用以下命令

make testdox # run tests without coverage reports but with prettified output

代码样式

您也可以使用以下命令运行代码样式检查

make phpcs

您也可以使用以下命令运行代码样式修复

make phpcsf

静态分析

要执行代码的静态分析(使用 phpstan,默认为 lvl 9),您可以使用以下命令

make analyze

最小支持版本

make php-min-compatibility

最大支持版本

make php-max-compatibility

CI 模拟

最后,“辅助”命令,您可以在提交和推送之前运行的是

make ci  

许可证

此项目受 MIT 许可证许可 - 有关详细信息,请参阅 LICENSE 文件