tibisoft/autotransformer
最新稳定版本:0.7.0
Composer 安装命令:
composer require tibisoft/autotransformer
包简介
A little tool for transforming DTO's to entities and viseversa
README 文档
README
Autotransformer is a little tool which does basic DTO to entity transforming and vise versa.
Installation
composer require tibisoft/autotransformer
Usage
Without any extra configuration the transformer will loop over the properties of the target class/object and will try to find a equally named property in the source.
class User { public function __construct( private string $email, private string $plainPassword, private int $age, ) { } } class UserDTO { public function __construct( private string $email, private int $age, ) { } } $user = new User('example@email.com', 'someSecretPassword', 25); $transformer = new \Tibisoft\AutoTransformer\AutoTransformer(); $userDTO = $transformer->transform($user, UserDTO::class); //output: object(UserDTO)#8 (2) { ["email":"UserDTO":private]=> string(17) "example@email.com" ["age":"UserDTO":private]=> int(25) }
Manipulate Transforming
If the default transforming process is not enough you can add attributes to the target object/class and customise the output.
Synonyms
class User { public function __construct( private string $email, private string $plainPassword, private int $age, ) { } } class UserDTO { public function __construct( private string $email, #[\Tibisoft\AutoTransformer\Attribute\Synonyms(['age'])] private int $yearsLive, ) { } } $user = new User('example@email.com', 'someSecretPassword', 25); $transformer = new \Tibisoft\AutoTransformer\AutoTransformer(); $userDTO = $transformer->transform($user, UserDTO::class); //output: object(UserDTO)#8 (2) { ["email":"UserDTO":private]=> string(17) "example@email.com" ["yearsLive":"UserDTO":private]=> int(25) }
Count
class User { public function __construct( private string $email, private array $comments, ) { } } class UserDTO { public function __construct( private string $email, #[\Tibisoft\AutoTransformer\Attribute\Count] private int $comments, ) { } } $user = new User('example@email.com', ['Comment #1', 'Comment #2', 'Comment #3', 'Comment #4']); $transformer = new \Tibisoft\AutoTransformer\AutoTransformer(); $userDTO = $transformer->transform($user, UserDTO::class); //output: object(UserDTO)#8 (2) { ["email":"UserDTO":private]=> string(17) "example@email.com" ["comments":"UserDTO":private]=> int(4) }
InArray
class InArrayClass { public function __construct( public array $roles = [], ) { } } class InArrayDTO { public function __construct( #[\Tibisoft\AutoTransformer\Attribute\InArray(property: 'roles', value: 'ROLE_TEAMLEADER')] public bool $isTeamleader, ) { } } $object = new InArrayClass(['ROLE_USER', 'ROLE_TEAMLEADER']); $transformer = new \Tibisoft\AutoTransformer\AutoTransformer(); $inArrayDTO = $transformer->transform($object, InArrayDTO::class); //output: object(InArrayDTO)#8 (1) { ["isTeamleader"]=> bool(true) }
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-08