PHP library for generating DTO classes.
Use the package manager composer to install micro/dto.
composer require micro/dto- example.xml
- See the full list of possible options in the XSD schema
<?xml version="1.0"?>
<dtoxmlns="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">
<classname="User\User">
<propertyname="email"type="string">
<validation>
<not_blank/>
<email/>
</validation>
</property>
<propertyname="username"type="string">
<validation>
<lengthmin="6"max="50"/>
<regexpattern="/^(.[aA-zA]+)$/"/>
</validation>
</property>
<propertyname="age"type="int">
<validation>
<not_blankgroups="put"/>
<greater_thanvalue="18" />
<less_thanvalue="100"groups="put, patch" />
</validation>
</property>
<propertyname="updatedAt"type="datetime" />
<propertyname="parent"type="User\User" /> </class>
</dto>- And run generator
$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);Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.