andreypostal/php-pancake-object
Composer 安装命令:
composer require andreypostal/php-pancake-object
包简介
Light and simple helper to work with value objects by providing a serializer and hydrator using PHP Attributes.
README 文档
README
Light and simple helper to work with value objects by providing a serializer and hydrator using PHP Attributes. It also provides a set of interfaces that allow you to customize the lib behavior to your needs.
Installation
composer require andreypostal/php-pancake-object
Usage
Attributes usage
Currently, we have three main attributes available:
- ValueObject
- Item
- SkipItem
When creating your Value Objects you just need to add the attribute Item
to each property that you want to hydrate/serialize.
use \Andrey\PancakeObject\Attributes\Item; // { "id": 123, "name": "my name" } class MyObject { #[Item] public int $id; #[Item(default: 'default name')] public string $name; }
In the case where you want to serialize/hydrate every property from an object
use \Andrey\PancakeObject\Attributes\ValueObject; // { "id": 123, "name": "my name" } #[ValueObject] class MyObject { public int $id; public string $name; }
You can also combine both when need to add custom key, make an item required or even just skip some property.
use \Andrey\PancakeObject\Attributes\ValueObject; use \Andrey\PancakeObject\Attributes\Item; use \Andrey\PancakeObject\Attributes\SkipItem; // { "id": 123, "custom_name": "my name" } #[ValueObject] class MyObject { public int $id; #[Item(key: 'custom_name')] public string $name; #[SkipItem] public string $ignoredProperty; }
In case the items are required to exist in the data array being used for hydration, you can add the required flag in the attribute to include some basic required validation.
use \Andrey\PancakeObject\Attributes\Item; // { "id": 123 } or { "id": 123, "name": "my name" } class MyObject { #[Item(required: true)] public int $id; #[Item] public string $name; }
When some of the keys in your data are different from your object, you can include the custom key in the attribute.
use \Andrey\PancakeObject\Attributes\Item; // { "customer_name": "the customer name" } class MyObject { #[Item(key: 'customer_name')] public string $name; }
Also, if you have a property that is an array of other object, you can inform the class in the attribute using the type option.
This will work as a hint so the hydrator can instantiate the appropriate object. This works with enums as well.
use \Andrey\PancakeObject\Item; use \MyNamespace\MyOtherObj; // { "list": [ { "key": "value" } ] } class MyObject { /** @var MyOtherObj[] */ #[Item(type: MyOtherObj::class)] public array $list; }
The type option can be used to validate that all the items in an array have some desired type as well, like "string", "integer"...
You can define a default value for an item using the "default" field when using the Item attribute:
use \Andrey\PancakeObject\Attributes\Item; class MyObject { #[Item] public int $id; #[Item(default: 'default name')] public string $name; }
Hydrator and Serializer
In order to hydrate some value object following the attribute rules, you just need to use the SimpleHydrator class. We also provide interfaces that allow you to customize the hydrator and key mapping strategy used (like snake case to pascal case).
The same applies to the SimpleSerializer class, used to serialize an object into an data array.
use \Andrey\PancakeObject\SimpleHydrator; use \Andrey\PancakeObject\SimpleSerializer; use \MyNamespace\MyObject; // Hydration phase $hydrator = new SimpleHydrator(); $dataArray = $request->body->toArray(); $myObject = $hydrator->hydrate($dataArray, MyObject::class); // Serialization phase $serializer = new SimpleSerializer(); $arr = $serializer->serialize($myObject);
andreypostal/php-pancake-object 适用场景与选型建议
andreypostal/php-pancake-object 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.12k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2025 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「serializer」 「hydrator」 「value-objects」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andreypostal/php-pancake-object 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andreypostal/php-pancake-object 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andreypostal/php-pancake-object 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bridge between JMS Serializer Bundle and Superdesk Web Publisher.
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Hydrator to map or extract an extract object
De/normalize business objects without tightly coupling them to your normalization format
A simple library that uses the hydration pattern to fill objects given different sources of data.
Doctrine ORM hydrators for Laminas
统计信息
- 总下载量: 2.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 12
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-21