micro / dto
生成和验证 DTO 类的库。
2.0.0-alpha
2024-07-06 20:35 UTC
Requires
- php: ^8.2
- ext-dom: *
- ext-libxml: *
- nette/php-generator: ^4
- psr/log: ^1 || ^2 || ^3
- symfony/property-access: ^5.4 || ^6
- symfony/validator: ^5.4 || ^6
Requires (Dev)
- symfony/expression-language: ^5.4 || ^6
- symfony/intl: ^5.4 || ^6
- symfony/var-dumper: ^5.4 || ^6
Suggests
- ext-intl: You need this PHP extension if you will be using `time_zone` alidator with option `intl_compatible`
- symfony/expression-language: You need this package if you will be using `Expression` validator
- symfony/intl: You need this package if you will be using `time_zone` alidator with option `intl_compatible`
This package is auto-updated.
Last update: 2024-09-06 21:08:39 UTC
README
用于生成 DTO 类的 PHP 库。
安装
使用包管理器 composer 安装 micro/dto。
composer require micro/dto
使用方法
在 XML Schemea 中声明所有所需的类
- example.xml
- 查看所有可能的选项的完整列表,请参阅 XSD 架构
<?xml version="1.0"?> <dto xmlns="micro:dto-1.6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="micro:dto-1.6 https://raw.githubusercontent.com/Micro-PHP/dto/master/src/Resource/schema/dto-1.6.xsd"> <class name="User\User"> <property name="email" type="string"> <validation> <not_blank/> <email/> </validation> </property> <property name="username" type="string"> <validation> <length min="6" max="50"/> <regex pattern="/^(.[aA-zA]+)$/"/> </validation> </property> <property name="age" type="int"> <validation> <not_blank groups="put"/> <greater_than value="18" /> <less_than value="100" groups="put, patch" /> </validation> </property> <property name="updatedAt" type="datetime" /> <property name="parent" type="User\User" /> </class> </dto>
- 并运行生成器
$classGenerator = new \Micro\Library\DTO\ClassGeneratorFacadeDefault( ['./example.xml'], // List of class declaration files './out', // Path to the folder where to generate 'Transfer' // Suffix for the all DTO classes (optional) ); $classGenerator->generate(); // Usage example $user = new \User\UserTransfer(); $user ->setAge(19) ->setEmail('demo@micro-php.net'); // OR // $user['age'] = 19; $user['email'] = 'demo@micro-php.net'; // Validation example $validator = new \Micro\Library\DTO\ValidatorFacadeDefault(); $validator->validate($user); // Validation groups by default ["Default"] $validator->validate($user, ['patch', 'put']); // Set validation groups ["patch", "put"] // Serialize example $serializer = new \Micro\Library\DTO\SerializerFacadeDefault(); $serializer->toArray($user); // Simple array $serializer->toJson($user); // Simple Json // Deserialize example $serialized = $serializer->toJsonTransfer($user); $deserialized = $serializer->fromJsonTransfer($serialized);
查看完整示例
贡献
欢迎拉取请求。对于重大更改,请首先打开一个问题来讨论您想要更改的内容。