An open API service indexing awesome lists of open source software.

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.

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/)