paolo / data-transfer-object
该包的最新版本(1.0.0)没有提供许可证信息。
简单的DTO包装器
1.0.0
2024-02-15 05:24 UTC
Requires
- php: >=8.1
Requires (Dev)
- laravel/pint: ^1.13
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
README
可用的属性
- PropertyIgnoreParse - 在解析数组时忽略对象属性
- PropertyIgnoreSerialize - 在序列化对象时忽略属性
示例
class T extends DataTransferObject { #[PropertyMapTo('test.job.0')] #[PropertyMapFrom('tt')] #[PropertyParse(T::class, 'parse', true)] #[PropertySerialize(T::class, 'serString', true, 'string')] #[PropertySerialize(T::class, 'serInt', true, PhpType::Integer)] #[PropertySerialize(T::class, 'serNull', true, PhpType::NULL)] public mixed $t; public static function parse(mixed $v) { return 'Hello world!'; } public static function serString(string $v) { return substr($v, 0, 5); } public static function serInt(int $v) { return $v * 2; } public static function serNull(mixed $v) { return 'undefined'; } } $t = T::wrap(['tt' => null]); var_dump($t->toArray()); /* array(1) { ["test"]=> array(1) { ["job"]=> array(1) { [0]=> string(5) "Hello" } } } */