morrislaptop/laravel-popo-caster
Composer 安装命令:
composer require morrislaptop/laravel-popo-caster
包简介
Automatically cast JSON columns to rich PHP objects in Laravel using Symfony's Serializer
README 文档
README
Laravel is awesome. Spatie's data transfer object package for PHP is awesome. But they don't cast objects like dates to DateTimes and collections are a bit of pain. Plain Old PHP Objects (POPOs) are a bit better in that regard.
Have you ever wanted to cast your JSON columns to a value object?
This package gives you 2 caster classes:
Serializerwhich serializes your value object and stores it in a single JSON fieldNormalizerwhich normalizes your value object and stores the properties as fields on your model
Under the hood it implements Laravel's Castable interface with a Laravel custom cast that handles serializing between the object (or a compatible array) and your JSON database column. It uses Symfony's Serializer to do this.
This package is inspired by Laravel Castable Data Transfer Object!
Installation
You can install the package via composer:
composer require morrislaptop/laravel-popo-caster
Serializer Usage
1. Create your POPO
namespace App\Values; class Address { public function __construct( public string $street, public string $suburb, public string $state, public Carbon $moved_at, ) { } }
2. Configure your Eloquent attribute to cast to it:
Note that this should be a jsonb or json column in your database schema. Objects and arrays are both supported.
namespace App\Models; use App\Values\Address; use Illuminate\Database\Eloquent\Model; use Morrislaptop\LaravelPopoCaster\Serializer; /** * @property Address $address */ class User extends Model { protected $casts = [ 'address' => Serializer::class . ':' . Address::class, 'prev_addresses' => Serializer::class . ':' . Address::class . '[]', ]; }
And that's it! You can now pass either an instance of your Address class, or even just an array with a compatible structure. It will automatically be cast between your class and JSON for storage and the data will be validated on the way in and out.
$user = User::create([ // ... 'address' => [ 'street' => '1640 Riverside Drive', 'suburb' => 'Hill Valley', 'state' => 'California', 'moved_at' => now(), ], 'addresses' => [ [ 'street' => '42 Wallaby Way', 'suburb' => 'Sydney', 'state' => 'NSW', 'moved_at' => '2020-01-14T00:00:00Z', ], ] ]) $residents = User::where('address->suburb', 'Hill Valley')->get();
But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.
$user->address->toMapUrl(); $user->address->getCoordinates(); $user->address->getPostageCost($sender); $user->address->calculateDistance($otherUser->address); $user->address->moved_at->diffForHumans(); echo (string) $user->address;
Normalizer Usage
1. Create your POPO
namespace App\Values; class Money { public function __construct( public int $amount, public string $currency, ) { } }
2. Configure your Eloquent attribute to cast to it:
Note that the properties of your value object should be columns in your database schema.
namespace App\Models; use App\Values\Money; use Illuminate\Database\Eloquent\Model; use Morrislaptop\LaravelPopoCaster\Normalizer; /** * @property Money $money */ class User extends Model { protected $casts = [ 'money' => Normalizer::class . ':' . Money::class, ]; }
And that's it! You can now pass either an instance of your Money class, or set the individual properties on the model. It will automatically be cast between your class and properties for storage and the data will be validated on the way in and out.
$user = User::create([ // ... 'amount' => 1000, 'curency' => 'AUD', ]); $user = User::create([ // ... 'money' => new Money(1000, 'AUD'), ])
But the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.
$user->money->convertTo('USD');
Plug
Want an easy way to mock or have factories for your POPOs? Check out morrislaptop/popo-factory
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
morrislaptop/laravel-popo-caster 适用场景与选型建议
morrislaptop/laravel-popo-caster 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 647.84k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2021 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「morrislaptop」 「laravel-popo-caster」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 morrislaptop/laravel-popo-caster 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 morrislaptop/laravel-popo-caster 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 morrislaptop/laravel-popo-caster 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Normalizers for common classes around the place
Partially boot Laravel for your lightning fast tests
Making it easy to mock your POPO's
A UI for Spatie's Laravel Event Sourcing
Your route:list, sir.
Make db:seed interactive
统计信息
- 总下载量: 647.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-31