承接 morrislaptop/laravel-popo-caster 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

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

Latest Version on Packagist GitHub Tests Action Status Total Downloads

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:

  • Serializer which serializes your value object and stores it in a single JSON field
  • Normalizer which 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 27
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-31