定制 kalimero/readonly-array-support 二次开发

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

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

kalimero/readonly-array-support

Composer 安装命令:

composer require kalimero/readonly-array-support

包简介

Laravel-style support for immutable toReadonlyArray pattern in DTOs and Resources.

README 文档

README

A lightweight PHP package for Laravel-style toReadonlyArray() support — perfect for DTO (Data Transfer Objects), JsonResource, Jobs, Events, or any class where you want to output an immutable array version of an object.

✨ Features

  • ToReadonlyArray interface for consistent API
  • CastsToReadonlyArray trait that automatically converts public readonly properties into array
  • ✅ Built for PHP 8.2+ and Laravel-style architecture
  • ✅ Ideal for Clean Architecture & modular DTO-based apps
  • ✅ Zero dependencies – ultra lightweight

🔧 Installation

composer require kalimero/readonly-array-support

⚠️ Requires PHP >= 8.2

🚀 Usage

1. Create a DTO

use Kalimero\ReadonlyArray\Contracts\ToReadonlyArray;
use Kalimero\ReadonlyArray\Traits\CastsToReadonlyArray;

readonly class ProductDTO implements ToReadonlyArray
{
    use CastsToReadonlyArray;

    public function __construct(
        public int $id,
        public string $name,
        public float $price,
    ) {}
}

2. Use in your application

$dto = new ProductDTO(1, 'T-Shirt', 29.99);

// Get as immutable array
$data = $dto->toReadonlyArray();

// Pass to Eloquent create
Product::create($data);

3. Works great for:

  • ✅ FormRequest -> DTO -> Model
  • ✅ Event Broadcasting
  • ✅ Logging/Audit trails
  • ✅ Queue Jobs
  • ✅ JsonResource transformation

💡 Why toReadonlyArray?

Laravel supports Arrayable, Responsable, JsonSerializable, etc. But sometimes you want to pass clean, readonly arrays between layers (DTOs, Jobs, APIs). This package gives you:

  • ✅ Consistent, immutable snapshot of data
  • ✅ DRY code – no more fromArray() or manual mapping
  • ✅ Works beautifully with Laravel mass-assignment

🧪 Testing

vendor/bin/phpunit

📄 License

MIT License © 2025 KalimeroMK

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-26