nandan108 / dto-toolkit
A modern, framework-agnostic PHP 8.1+ DTO system with attribute-driven data normalization and mapping to entities or arrays.
Requires
- php: >=8.1 <8.5
Requires (Dev)
- php: >=8.1 <8.5
- friendsofphp/php-cs-fixer: ^3.75
- mockery/mockery: ^1.6
- nunomaduro/collision: ^6.4
- phpunit/phpunit: ^10.0
- symfony/var-dumper: ^6.0 || ^7.0
- vimeo/psalm: ^6.10
README
✅ Requires PHP 8.1–8.4
DTO Toolkit Core is a lightweight, framework-agnostic library for defining, transforming, and (soon) validating Data Transfer Objects (DTOs) in PHP.
It offers a clean, declarative API powered by attributes — handling normalization, casting, and output shaping — all without coupling to any specific framework.
Casters, modifiers, and validators are composed into a fully declarative transformation DSL, JIT-compiled into efficient processing chains at runtime.
✨ Features
- 🧱 Minimal and framework-agnostic
- 🏷️ Attribute-based casting system with flexible resolution
- 🎯 Optional validation and normalization layers
- 🔄 Easily transform between DTOs and entities/models
- 🧩 Designed to work with pluggable framework adapters (Laravel, Symfony, etc.)
📦 Installation
composer require nandan108/dto-toolkit
🚀 Quick Start
use Nandan108\DtoToolkit\Core\{FullDto, CastTo}; // FullDto includes all standard traits (CreatesFromArray, NormalizesFromAttributes, ExportsToEntity) class MyDto extends FullDto { #[CastTo\Trimmed()] public ?string $name = null; } // Build DTO from array $dto = MyDto::fromArray(['name' => ' Alice ']); // Transform into an entity $entity = $dto->toEntity();
Use a framework adapter (e.g. Symfony or Laravel) to unlock request/response integration and validation support.
📦 Core Namespace
If you're not using a framework, start with:
Nandan108\DtoToolkit\Core\FullDto
Nandan108\DtoToolkit\Core\CastTo
These provide a convenient, framework-free entry point with all standard functionality included.
📚 Documentation
- DTO Fundamentals – what DTOs are, why they matter, and how to use them in modern PHP
- Casting – how casting works and how to write your own
- Lifecycle – Understanding the lifecycle of a DTO
- Lifecycle Hooks – customize behavior with
postLoad()
andpreOutput()
- Toolkit Comparison – see how this toolkit compares to other PHP DTO/mapping libraries
- Built-In Casters — Full list of available
CastTo\*
casters - Built-In Modifiers — Full list of available
Mod\*
chain modifiers - Dependency Injection
- Validation (planned)
- Writing Adapters (planned)
🧩 Adapter Packages
- Laravel Adapter:
nandan108/dto-toolkit-laravel
(planned) - Symfony Adapter:
nandan108/dto-toolkit-symfony
(planned)
Adapters will provide support for:
- Validation using framework services
fromRequest()
DTO hydrationtoEntity()
ortoModel()
adapter-specific hydrationtoResponse()
generation- DI for class-based casters resolution
- Graceful handling of validation and casting exceptions in HTTP contexts, with standardized API error responses
🧪 Testing & Quality
- 100% test coverage using PHPUnit
- Static analysis via Psalm (
--show-info=true
clean)
🤝 Contributing
Bug reports, ideas, and contributions welcome! This project aims to stay lean, clean, and focused. If you're building an adapter or extending the system, feel free to open a discussion or issue.
🧭 Design Philosophy
This toolkit is built on a simple idea: do the most work with the least number of moving parts.
It favors:
- Declarative code over procedural boilerplate
- Clever, expressive syntax without falling into obfuscation
- Minimalism with power — clean by default, extensible when needed
- Framework-agnostic design with optional adapters that integrate smoothly when needed
- Zero magic in the core — everything is traceable, explicit, and predictable
- Opt-in magic in adapters — for just the right touch of convenience when working with frameworks
- Separation of concerns and composability — inspired by the Unix philosophy of doing one thing well
- Performance-conscious by design — DTOs should feel lightweight and fast to use, even in large batch transformations.
Verbose code is a tax on your time. This toolkit aims to keep things sharp, concise, and purposeful — so you can focus on what matters.
MIT License © nandan108