jooservices / dto
A PHP 8.5+ DTO and Data library with immutable DTOs and mutable Data objects
Requires
- php: >=8.5
Requires (Dev)
- captainhook/captainhook: ^5.23
- captainhook/plugin-composer: ^5.3
- fakerphp/faker: ^1.24
- friendsofphp/php-cs-fixer: ^3.65
- laravel/pint: ^1.18
- phpmd/phpmd: ^2.15
- phpstan/phpstan: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.0
- squizlabs/php_codesniffer: ^3.8
- dev-master
- 1.0.1
- 1.0.0
- dev-develop
- dev-docs/architecture-docs-refresh
- dev-dependabot/github_actions/ramsey/composer-install-4
- dev-dependabot/composer/dev-dependencies-34d8bc73d9
- dev-dependabot/github_actions/actions/upload-artifact-7
- dev-dependabot/github_actions/actions/checkout-6
- dev-dependabot/github_actions/codecov/codecov-action-5
- dev-dependabot/github_actions/github/codeql-action-4
This package is auto-updated.
Last update: 2026-03-24 07:32:10 UTC
README
A modern PHP 8.5+ library for building type-safe, immutable Data Transfer Objects (DTOs) with powerful hydration, validation, and transformation capabilities.
✨ Key Features
| Feature | Description |
|---|---|
| 🔒 Immutable DTOs | Type-safe, readonly data transfer objects |
| ✏️ Mutable Data Objects | Flexible mutable data containers via Data class |
| 💧 Multi-Source Hydration | Create from arrays, JSON, objects with from(), fromArray(), fromJson() |
| 🔄 Type Casting | Automatic type conversion (scalars, DateTime, Enums, nested DTOs, arrays) |
| ✅ Validation | Built-in validation attributes (@Required, @Email, @Valid) |
| 🎨 Custom Casters | Extensible type casting system via CasterInterface |
| 🔧 Custom Transformers | Output transformation pipeline with TransformerInterface |
| 📝 Naming Strategies | Automatic camelCase ↔ snake_case conversion |
| 📚 Collections | DataCollection and PaginatedCollection support |
| 📋 Schema Generators | Export to JSON Schema and OpenAPI 3.0 specifications |
| ⚡ Pipeline System | Global and per-property data transformation pipelines |
| 🛠️ Utility Methods | diff(), equals(), hash(), merge(), clone(), when(), unless() |
| 🔀 Optional | Type-safe optional value wrapper for handling missing data |
| 🎯 Lifecycle Hooks | transformInput(), afterHydration(), beforeSerialization() |
| ⚡ Computed Properties | WeakMap-based cached lazy-evaluated properties |
| 🏗️ Polymorphism | @DiscriminatorMap for polymorphic type mapping |
🚀 Quick Start
Installation
composer require jooservices/dto
Requirements: PHP 8.5 or higher
Basic Usage
use JOOservices\Dto\Core\Dto; use JOOservices\Dto\Attributes\MapFrom; use JOOservices\Dto\Attributes\Validation\Required; class UserDto extends Dto { public function __construct( #[Required] public readonly string $name, #[MapFrom('email_address')] public readonly string $email, public readonly int $age, ) {} } // Create from array $user = UserDto::from([ 'name' => 'John Doe', 'email_address' => 'john@example.com', 'age' => 30 ]); // Access properties echo $user->name; // John Doe echo $user->email; // john@example.com // Convert to array $array = $user->toArray(); // Convert to JSON $json = $user->toJson();
Advanced Example: Nested DTOs and Collections
class AddressDto extends Dto { public function __construct( public readonly string $street, public readonly string $city, public readonly string $country, ) {} } class UserProfileDto extends Dto { public function __construct( public readonly string $name, public readonly AddressDto $address, /** @var array<TagDto> */ public readonly array $tags, ) {} } $profile = UserProfileDto::from([ 'name' => 'Jane Smith', 'address' => [ 'street' => '123 Main St', 'city' => 'New York', 'country' => 'USA' ], 'tags' => [ ['name' => 'developer'], ['name' => 'php'] ] ]);
📖 Documentation
Comprehensive documentation is available in the ./docs directory:
- Getting Started - Installation, quick start, basic concepts
- User Guide - Creating DTOs, validation, best practices, troubleshooting
- Examples - Real-world usage examples and API integration patterns
👉 Start here: Documentation Hub
🎯 Use Cases
- REST API Development - Type-safe request/response handling
- Form Processing - Validate and transform user input
- Database Mapping - Map database records to typed objects
- Microservices - Consistent data contracts between services
- Data Validation - Ensure data integrity with built-in validation
- API Clients - Type-safe API response objects
🔗 Links
| Resource | URL |
|---|---|
| 📦 Packagist | packagist.org/packages/jooservices/dto |
| 💻 GitHub | github.com/jooservices/dto |
| 🐛 Issue Tracker | github.com/jooservices/dto/issues |
| 📧 Contact | contact@jooservices.com |
🤝 Contributing
Contributions are welcome! Please see our contributing guidelines in the documentation for details on:
- Code style and standards
- Running tests
- Submitting pull requests
- Reporting issues
📜 License
This library is open-sourced software licensed under the MIT license.
🙏 Credits
Developed and maintained by JOOservices
Special thanks to all contributors who have helped improve this library.
Ready to build type-safe DTOs? → Get Started