https://github.com/micro-php/dto
PHP library for generating DTO classes.
https://github.com/micro-php/dto
dto dto-generator dto-pattern microphp
Last synced: 19 days ago
JSON representation
PHP library for generating DTO classes.
- Host: GitHub
- URL: https://github.com/micro-php/dto
- Owner: Micro-PHP
- License: mit
- Created: 2022-05-11T08:28:13.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-06T20:51:22.000Z (11 months ago)
- Last Synced: 2025-04-25T12:47:53.063Z (29 days ago)
- Topics: dto, dto-generator, dto-pattern, microphp
- Language: PHP
- Homepage:
- Size: 188 KB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DTO Generator
PHP library for generating DTO classes.
## Installation
Use the package manager [composer](https://getcomposer.org/) to install micro/dto.
```bash
composer require micro/dto
```## Usage
#### Declare all required classes in the XML Schemea
* example.xml
* See the full list of possible options in the [XSD schema](src/Resource/schema/dto-1.6.xsd)``` xml
```
* And run generator
```php
$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('[email protected]');
// OR
//
$user['age'] = 19;
$user['email'] = '[email protected]';// 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);```
### [See full example](./example/)
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.## License
[MIT](https://choosealicense.com/licenses/mit/)