alamellama/carapace
Composer 安装命令:
composer require alamellama/carapace
包简介
Framework-agnostic DTOs for PHP
README 文档
README
Carapace is a lightweight PHP library for building immutable, strictly typed Data Transfer Objects (DTOs). It leverages PHP attributes for casting, property mapping, and serialization, while providing a simple, expressive API.
Features
- Mutable + Immutable DTOs: Define data objects by extending the Data base class, or use ImmutableData for readonly DTOs.
- Attribute-Driven Mapping: Use PHP attributes like CastWith, MapFrom, MapTo, and Hidden to control how data is hydrated, transformed, and serialized with minimal boilerplate.
- Strictly Typed: Leverage PHP's type system for predictable data structures.
- Framework-Agnostic: Works in Laravel, Symfony, or plain PHP projects.
- Simple API: Create, hydrate, and transform DTOs with minimal boilerplate.
Installation
composer require alamellama/carapace
Usage
use Alamellama\Carapace\Data; use Alamellama\Carapace\Attributes\CastWith; use Alamellama\Carapace\Attributes\Hidden; use Alamellama\Carapace\Attributes\MapFrom; class User extends Data { public function __construct( public string $name, #[MapFrom('email_address')] public string $email, #[Hidden] public string $password, #[CastWith(Address::class)] public Address $address, ) {} } // Create from an array $user = User::from([ 'name' => 'John Doe', 'email_address' => 'john@example.com', 'password' => 'secret', 'address' => [ 'street' => '123 Main St', 'city' => 'Anytown', ], ]); // Create a modified copy $updatedUser = $user->with(name: 'Jane Doe'); // Serialize $array = $user->toArray(); // Password will be excluded $json = $user->toJson();
Documentation
For detailed documentation, visit our documentation site.
Code Quality
Carapace follows PSR-12 coding standards with Laravel-style modifications. We use Laravel Pint for code style enforcement and Rector for automated refactoring. PHPStan is configured at a high strictness level.
# Apply automated fixes (Pint + Rector) composer fix # Run tests and quality checks composer test
Testing
Carapace uses Pest PHP for testing and aims for 100% test coverage.
# Run all tests composer test
Acknowledgements & Inspirations
In particular, we drew inspiration and ideas from:
- Spatie's data libraries, including spatie/data-transfer-object and spatie/laravel-data
- CuyZ/Valinor
- Symfony Serializer
We also rely on fantastic tooling that keeps this project reliable and maintainable:
- Pest PHP for testing
- PHPStan for static analysis
- Laravel Pint for code style
- Rector for automated refactoring
License
Carapace is open-sourced software licensed under the MIT license. See the LICENSE file for details.
alamellama/carapace 适用场景与选型建议
alamellama/carapace 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.41k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2025 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 alamellama/carapace 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alamellama/carapace 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-05
