Download the PHP package php-collective/dto without Composer
On this page you can find all versions of the php package php-collective/dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download php-collective/dto
More information about php-collective/dto
Files in php-collective/dto
Package dto
Short Description Framework-agnostic Data Transfer Object library with code generation
License MIT
Homepage https://php-collective.github.io/dto/
Informations about the package dto
PHP Data Transfer Objects
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 for why code generation beats runtime reflection.
Installation
Quick Start
Define DTOs in PHP (or XML, YAML, NEON):
Generate and use:
See the documentation for detailed examples.
Immutable DTOs
A more realistic example using immutable DTOs for a blog system:
Reading in a template (e.g., Twig, Blade, or plain PHP):
Features
- Types:
int,float,string,bool,array,mixed, DTOs, classes, enums - Union types:
int|string,int|float|string - Collections:
'type' => 'Item[]', 'collection' => truewith add/remove/get/has methods - Associative collections: keyed access with
'associative' => true - Immutable DTOs:
'immutable' => truewithwith*()methods - Readonly properties:
public readonlywith direct property access - Validation rules: built-in
minLength,maxLength,min,max,patternconstraints - 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<int, ItemDto>for static analysis - TypeScript generation: Generate TypeScript interfaces from your DTO configs
- Schema Importer: Auto-create DTOs from JSON data or JSON Schema
Configuration Formats
- PHP - native arrays or fluent builder
- 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:
Options: --readonly, --strict-nulls, --file-case=pascal|dashed|snake
See TypeScript Generation for full documentation.
Schema Importer
Bootstrap DTO configurations from existing JSON data or JSON Schema definitions:
Outputs to PHP, XML, YAML, or NEON format. Perfect for integrating with external APIs.
See Schema Importer for full documentation.
Documentation
Full documentation available at php-collective.github.io/dto
- Getting Started - Quick start guide with examples
- Configuration Builder - Fluent API for defining DTOs
- Runtime API - Core DTO methods and global runtime options
- Examples - Practical usage patterns
- Circular Dependencies - How generation-time cycle detection works
- TypeScript Generation - Generate TypeScript interfaces
- JSON Schema Generation - Generate JSON Schema for DTO definitions
- Schema Importer - Bootstrap DTOs from JSON data/schema
- Performance - Benchmarks and optimization tips
Integrations
This is the standalone core library. For framework-specific integrations:
- CakePHP: dereuromark/cakephp-dto - Bake commands, plugin
- Laravel: php-collective/laravel-dto - Artisan commands, service provider
- Symfony: php-collective/symfony-dto - Console commands, bundle integration
All versions of dto with dependencies
sebastian/diff Version ^6.0 || ^7.0 || ^8.0 || ^9.0
twig/twig Version ^3.0