lastdragon-ru/lara-asp-serializer
Composer 安装命令:
composer require lastdragon-ru/lara-asp-serializer
包简介
Provides a customizable wrapper around the Symfony Serializer Component to use it inside Laravel application.
README 文档
README
This package provides a customizable wrapper around the Symfony Serializer Component to use it inside Laravel application.
Requirements
| Requirement | Constraint | Supported by |
|---|---|---|
| PHP | ^8.5 |
HEAD ⋯ 11.0.0 |
^8.4 |
HEAD ⋯ 8.0.0 |
|
^8.3 |
10.3.0 ⋯ 5.0.0 |
|
^8.2 |
7.2.0 ⋯ 5.0.0-beta.0 |
|
^8.1 |
6.4.2 ⋯ 5.0.0-beta.0 |
|
| Laravel | ^13.10.0 |
HEAD , 11.1.0 |
^13.0.0 |
11.0.0 |
|
^12.0.1 |
10.3.0 ⋯ 9.0.0 |
|
^11.0.8 |
8.1.1 ⋯ 8.0.0 |
|
^11.0.0 |
7.2.0 ⋯ 6.2.0 |
|
^10.34.0 |
7.2.0 ⋯ 6.2.0 |
|
^10.0.0 |
6.1.0 ⋯ 5.0.0-beta.0 |
|
^9.21.0 |
5.6.0 ⋯ 5.0.0-beta.1 |
|
^9.0.0 |
5.0.0-beta.0 |
Installation
composer require lastdragon-ru/lara-asp-serializer
Usage
<?php declare(strict_types = 1); // phpcs:disable PSR1.Files.SideEffects // phpcs:disable PSR1.Classes.ClassDeclaration namespace LastDragon_ru\LaraASP\Serializer\Docs\Examples\Usage; use DateTimeInterface; use Illuminate\Support\Facades\Date; use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable; use LastDragon_ru\LaraASP\Serializer\Contracts\Serializer; class User implements Serializable { public function __construct( public int $id, public string $name, public DateTimeInterface $created, ) { // empty } } $user = new User(1, 'User', Date::parse('2023-08-27T08:30:44.473+00:00')); $serializer = app()->make(Serializer::class); $serialized = $serializer->serialize($user); $deserialized = $serializer->deserialize(User::class, $serialized); Example::dump($serialized); Example::dump($deserialized);
The $serialized is:
"{"id":1,"name":"User","created":"2023-08-27T08:30:44.473+00:00"}"
The $deserialized is:
LastDragon_ru\LaraASP\Serializer\Docs\Examples\Usage\User {
+id: 1
+name: "User"
+created: Illuminate\Support\Carbon {
+"date": "2023-08-27 08:30:44.473000"
+"timezone_type": 1
+"timezone": "+00:00"
}
}
Partial deserialization
Sometimes you don't know (or do not want to support) the full structure of the object. In this case you can mark the class as Partial to allow unserialize only known (wanted) properties:
<?php declare(strict_types = 1); // phpcs:disable PSR1.Files.SideEffects // phpcs:disable PSR1.Classes.ClassDeclaration namespace LastDragon_ru\LaraASP\Serializer\Docs\Examples\Partial; use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Serializer\Contracts\Partial; use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable; use LastDragon_ru\LaraASP\Serializer\Contracts\Serializer; class User implements Serializable, Partial { public function __construct( public string $name, ) { // empty } } $serializer = app()->make(Serializer::class); $deserialized = $serializer->deserialize(User::class, '{"id":1,"name":"User"}'); Example::dump($deserialized);
The $deserialized is:
LastDragon_ru\LaraASP\Serializer\Docs\Examples\Partial\User {
+name: "User"
}
Extending
Out of the box, the package supports only the following objects (see Factory for more details):
- Any object that implement
Serializable(seeSerializableNormalizer) - Any object that implement
\DateTimeInterface(seeDateTimeNormalizer) \DateTimeZone\DateInterval- PHP Enums
Publish the config and add normalizers/denormalizers if you need more:
php artisan vendor:publish --provider=LastDragon_ru\\LaraASP\\Serializer\\PackageProvider --tag=config
Eloquent Accessor/Mutator1
You can use the Serialized attribute to populate a model attribute to/from an object:
<?php declare(strict_types = 1); // phpcs:disable PSR1.Files.SideEffects // phpcs:disable PSR1.Classes.ClassDeclaration namespace LastDragon_ru\LaraASP\Serializer\Docs\Examples\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Serializer\Casts\Serialized; use LastDragon_ru\LaraASP\Serializer\Contracts\Serializable; class UserSettings implements Serializable { public function __construct( public int $perPage, public bool $showSidebar, ) { // empty } } class User extends Model { /** * @return Attribute<?UserSettings, ?UserSettings> */ protected function settings(): Attribute { return app()->make(Serialized::class)->attribute(UserSettings::class); } } $user = new User(); $user->settings = new UserSettings(35, false); Example::dump($user->settings); Example::dump($user->getAttributes());
The $user->settings is:
LastDragon_ru\LaraASP\Serializer\Docs\Examples\Attribute\UserSettings {
+perPage: 35
+showSidebar: false
}
The $user->getAttributes() is:
[
"settings" => "{"perPage":35,"showSidebar":false}",
]
Upgrading
Please follow Upgrade Guide.
Contributing
Please use the main repository to report issues, send pull requests, or ask questions.
Footnotes
lastdragon-ru/lara-asp-serializer 适用场景与选型建议
lastdragon-ru/lara-asp-serializer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73.07k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「php」 「serializer」 「laravel」 「symfony-serializer」 「laravel-package」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lastdragon-ru/lara-asp-serializer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lastdragon-ru/lara-asp-serializer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lastdragon-ru/lara-asp-serializer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bridge between JMS Serializer Bundle and Superdesk Web Publisher.
De/normalize business objects without tightly coupling them to your normalization format
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Smokescreen is a PHP library for transforming and serializing data such as API responses.
provides an facade for php-yaml, syck and symphony/yaml so switching in between is easier
统计信息
- 总下载量: 73.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-17