https://github.com/php-collective/dto
The easiest and fastest PHP DTO library in the world - quickly generate useful data transfer objects for your app (mutable/immutable)
https://github.com/php-collective/dto
dto dto-entity-mapper dto-pattern
Last synced: 4 months ago
JSON representation
The easiest and fastest PHP DTO library in the world - quickly generate useful data transfer objects for your app (mutable/immutable)
- Host: GitHub
- URL: https://github.com/php-collective/dto
- Owner: php-collective
- License: mit
- Created: 2025-12-14T01:08:46.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2026-02-06T14:06:27.000Z (5 months ago)
- Last Synced: 2026-02-06T22:26:46.902Z (5 months ago)
- Topics: dto, dto-entity-mapper, dto-pattern
- Language: PHP
- Homepage:
- Size: 472 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Data Transfer Objects
[](https://github.com/php-collective/dto/actions/workflows/ci.yml)
[](https://codecov.io/gh/php-collective/dto)
[](https://phpstan.org/)
[](https://packagist.org/packages/php-collective/dto)
[](https://php.net)
[](LICENSE)
Framework-agnostic DTO library with **code generation** for PHP.
Unlike runtime reflection libraries, this library generates optimized DTO classes at build time, giving you:
- **Zero runtime reflection overhead**
- **Perfect IDE autocomplete** with real methods
- **Excellent static analysis** support (PHPStan/Psalm work out of the box)
- **Reviewable generated code** in pull requests
- **JSON Schema generation** for API documentation
- **Schema importer** to bootstrap DTOs from JSON data or OpenAPI specs
See [Motivation](docs/Motivation.md) for why code generation beats runtime reflection.
## Installation
```bash
composer require php-collective/dto
```
## Quick Start
Define DTOs in PHP (or XML, YAML, NEON):
```php
// config/dto.php
use PhpCollective\Dto\Config\Dto;
use PhpCollective\Dto\Config\Field;
use PhpCollective\Dto\Config\Schema;
return Schema::create()
->dto(Dto::create('Car')->fields(
Field::string('color'),
Field::dto('owner', 'Owner'),
))
->dto(Dto::create('Owner')->fields(
Field::string('name'),
))
->toArray();
```
Generate and use:
```bash
vendor/bin/dto generate
```
```php
$car = CarDto::createFromArray(['color' => 'red']);
$car->setOwner(OwnerDto::create(['name' => 'John']));
$array = $car->toArray();
```
See [Quick Start Guide](docs/README.md) for detailed examples.
## Immutable DTOs
A more realistic example using immutable DTOs for a blog system:
```php
// config/dto.php
return Schema::create()
->dto(Dto::immutable('Article')->fields(
Field::int('id')->required(),
Field::string('title')->required(),
Field::string('slug')->required(),
Field::string('content'),
Field::dto('author', 'Author')->required(),
Field::collection('tags', 'Tag')->singular('tag'),
Field::bool('published')->defaultValue(false),
Field::string('publishedAt'),
))
->dto(Dto::immutable('Author')->fields(
Field::string('name')->required(),
Field::string('email'),
Field::string('avatarUrl'),
))
->dto(Dto::immutable('Tag')->fields(
Field::string('name')->required(),
Field::string('slug')->required(),
))
->toArray();
```
```php
// Creating from API/database response
$article = ArticleDto::createFromArray($apiResponse);
```
Reading in a template (e.g., Twig, Blade, or plain PHP):
```php
= htmlspecialchars($article->getTitle()) ?>
= $article->getContent() ?>
```
## Features
- **Types**: `int`, `float`, `string`, `bool`, `array`, `mixed`, DTOs, classes, enums
- **Union types**: `int|string`, `int|float|string`
- **Collections**: `'type' => 'Item[]', 'collection' => true` with add/remove/get/has methods
- **Associative collections**: keyed access with `'associative' => true`
- **Immutable DTOs**: `'immutable' => true` with `with*()` methods
- **Readonly properties**: `public readonly` with direct property access
- **Validation rules**: built-in `minLength`, `maxLength`, `min`, `max`, `pattern` constraints
- **Lazy properties**: deferred DTO/collection hydration with `asLazy()`
- **Default values**: `'defaultValue' => 0`
- **Required fields**: `'required' => true`
- **Deprecations**: `'deprecated' => 'Use newField instead'`
- **Inflection**: automatic snake_case/camelCase/dash-case conversion
- **Deep cloning**: `$dto->clone()`
- **Nested reading**: `$dto->read(['path', 'to', 'field'])`
- **PHPDoc generics**: `@var \ArrayObject` for static analysis
- **[TypeScript generation](#typescript-generation)**: Generate TypeScript interfaces from your DTO configs
- **[Schema Importer](#schema-importer)**: Auto-create DTOs from JSON data or JSON Schema
## Configuration Formats
- **PHP** - native arrays or [fluent builder](docs/ConfigBuilder.md)
- **XML** - XSD validation, IDE autocomplete
- **YAML** - requires `pecl install yaml`
- **NEON** - requires `nette/neon`
## TypeScript Generation
Generate TypeScript interfaces directly from your DTO configuration - keeping frontend and backend types in sync:
```bash
# Single file output
vendor/bin/dto typescript --config-path=config/ --output=frontend/src/types/
# Multi-file with separate imports
vendor/bin/dto typescript --multi-file --file-case=dashed --output=types/
```
```typescript
// Generated: types/dto.ts
export interface UserDto {
id: number;
name: string;
email: string;
address?: AddressDto;
roles?: RoleDto[];
}
```
Options: `--readonly`, `--strict-nulls`, `--file-case=pascal|dashed|snake`
See [TypeScript Generation](docs/TypeScriptGeneration.md) for full documentation.
## Schema Importer
Bootstrap DTO configurations from existing JSON data or JSON Schema definitions:
```php
use PhpCollective\Dto\Importer\Importer;
$importer = new Importer();
// From API response example
$json = '{"name": "John", "age": 30, "email": "john@example.com"}';
$config = $importer->import($json);
// From JSON Schema
$schema = file_get_contents('openapi-schema.json');
$config = $importer->import($schema, ['format' => 'xml']);
```
Outputs to PHP, XML, YAML, or NEON format. Perfect for integrating with external APIs.
See [Schema Importer](docs/Importer.md) for full documentation.
## Documentation
- [Quick Start Guide](docs/README.md) - Getting started with examples
- [Configuration Builder](docs/ConfigBuilder.md) - Fluent API for defining DTOs
- [Examples](docs/Examples.md) - Practical usage patterns
- [TypeScript Generation](docs/TypeScriptGeneration.md) - Generate TypeScript interfaces
- [Schema Importer](docs/Importer.md) - Bootstrap DTOs from JSON data/schema
- [Performance](docs/Performance.md) - Benchmarks and optimization tips
- [Motivation](docs/Motivation.md) - Why code generation beats runtime reflection
## Integrations
This is the standalone core library. For framework-specific integrations:
- **CakePHP**: [dereuromark/cakephp-dto](https://github.com/dereuromark/cakephp-dto) - Bake commands, plugin
- **Laravel**: [php-collective/laravel-dto](https://github.com/php-collective/laravel-dto) - Artisan commands, service provider
- **Symfony**: [php-collective/symfony-dto](https://github.com/php-collective/symfony-dto) - Console commands, bundle integration