定制 done-super-app/cereal 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

done-super-app/cereal

Composer 安装命令:

composer require done-super-app/cereal

包简介

A package to simplify serialization management.

README 文档

README

Cereal package logo
Cereal

Tests Total Downloads License

Cereal is a PHP package that allows you to easily serialize and deserialize your data.

Installation

You can install the package via composer:

composer require done-super-app/cereal

Usage

To start using the package, you need to create a class that implements the DoneSuperApp\Cereal\Contracts\Serializable interface, and use the DoneSuperApp\Cereal\Cereal trait which contains some helpers for a default configuration.

class User implements Serializable
{
    use Cereal;

    public int $age;
    public string $name;
    public float $weight;
    public bool $isStraight;

    public function __construct(
        string $name,
        int $age,
        float $weight,
        bool $isStraight
    ) {
        $this->age = $age;
        $this->name = $name;
        $this->weight = $weight;
        $this->isStraight = $isStraight;
    }

    public function serializes(): array
    {
        return ['name', 'age', 'weight', 'isStraight'];
    }
}

The serializes method returns an array of the properties that you want to serialize. Then the package will automatically serialize and deserialize the data based on the type hinted properties.

Note

  • The package will only serialize public properties that are type hinted.
  • The package will serialize properties in the order they are defined in the serialize method.

Serialization Handlers

In most uses cases, you will need to serialize and deserialize some properties in a different way. For example, you may want to serialize a DateTime object to a timestamp, or serialize a User object to an array or something. For that, you're going to create your own serialization handler and add it to the DoneSuperApp\Cereal\SerializationHandlerFactory.

class UserHandler implements SerializationHandler {

    protected UserRepository $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function serialize(Serializable $serializable, $value): string
    {
        return $value->getId();
    }

    /**
     * @param string $value
     *
     * @return ?User
     */
    public function deserialize(Serializable $serializable, $value): ?User
    {
        return $this->userRepository->find($value);
    }
}

In the above example, when trying to serialize a user object, we serialize only the ID, and when trying to deserialize it, we fetch it again from database.

Then, you need to add your handler to the DoneSuperApp\Cereal\SerializationHandlerFactory:

use DoneSuperApp\Cereal\SerializationHandlerFactory;

SerializationHandlerFactory::getInstance()
    ->addHandler(User::class, new UserHandler($userRepository));

Now, when working with our object that will be serialized, and if it contains a User instance, it will automatically be serialized and deserialized using our handler.

class Post implements Serializable
{
    use SerializeTrait;

    public string $title;
    public string $content;
    
    public User $author;

    public function __construct(string $title, string $content, User $author)
    {
        $this->title = $title;
        $this->content = $content;
        $this->author = $author;
    }

    public function serializes(): array
    {
        return ['title', 'content', 'author'];
    }
}

Laravel Bridge

// TODO

done-super-app/cereal 适用场景与选型建议

done-super-app/cereal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.83k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 done-super-app/cereal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 done-super-app/cereal 我们能提供哪些服务?
定制开发 / 二次开发

基于 done-super-app/cereal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-18