soyhuce / data-transfer-object-casts
spatie/data-transfer-object 的常用转换
0.1.1
2022-08-29 06:51 UTC
Requires
- php: ^8.1
- nesbot/carbon: ^2.53
- spatie/data-transfer-object: ^3.8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- larapack/dd: ^1.1
- pestphp/pest: ^1.21
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-09 17:58:25 UTC
README
spatie/data-transfer-object 的常用转换
安装
您可以通过 composer 安装此包
composer require soyhuce/data-transfer-object-casts
用法
BooleanCaster
将输入转换为布尔值,如果适用。
use Soyhuce\DataTransferObjectCasts\BooleanCaster; use Spatie\DataTransferObject\Attributes\DefaultCast; use Spatie\DataTransferObject\DataTransferObject; #[DefaultCast('bool', BooleanCaster::class)] class MyDTO extends DataTransferObject { public bool $bool; } $dto = new MyDTO( bool: 'true', ); $dto->bool; // true
CarbonImmutableCaster
将输入转换为 CarbonImmutable 实例。如果输入不是字符串,则返回原值。
默认格式为 '!Y-m-d H:i:s'
。
use Carbon\CarbonImmutable; use Soyhuce\DataTransferObjectCasts\CarbonImmutableCaster; use Spatie\DataTransferObject\Attributes\CastWith; use Spatie\DataTransferObject\Attributes\DefaultCast; use Spatie\DataTransferObject\DataTransferObject; #[DefaultCast(CarbonImmutable::class, CarbonImmutableCaster::class)] class MyDTO extends DataTransferObject { public CarbonImmutable $dateTime; #[CastWith(CarbonImmutableCaster::class, '!Y-m-d')] public CarbonImmutable $date; } $dto = new MyDTO( dateTime: '2022-08-11 14:44:45', date: '2022-08-01', ); $dto->dateTime; // CarbonImmutable instance $dto->date; // CarbonImmutable instance
StringEnumCaster
将输入转换为受支持的字符串枚举。
use Soyhuce\DataTransferObjectCasts\StringEnumCaster; use Spatie\DataTransferObject\Attributes\CastWith; use Spatie\DataTransferObject\DataTransferObject; #[CastWith(StringEnumCaster::class)] enum StringEnum: string { case ok = 'ok'; case nok = 'nok'; } class MyDTO extends DataTransferObject { public StringEnum $stringEnum; } $dto = new MyDTO( stringEnum: 'ok', ); $dto->stringEnum; // StringEnum::ok
测试
composer test
变更日志
请参阅 变更日志 了解最近的变化。
贡献
请参阅 贡献指南 了解详情。
安全漏洞
请查阅 我们的安全策略 了解如何报告安全漏洞。
鸣谢
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 了解更多信息。