✅ 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.
- 🧱 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.)
composer require nandan108/dto-toolkituse 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.
If you're not using a framework, start with:
Nandan108\DtoToolkit\Core\FullDtoNandan108\DtoToolkit\Core\CastTo
These provide a convenient, framework-free entry point with all standard functionality included.
- 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)
- 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
- 100% test coverage using PHPUnit
- Psalm level 3
- Code style enforced with PHP-CS-Fixer:
- Based on the
@Symfonyrule set - Aligned
=>for better readability - Disallows implicit loose comparisons (
==,!=)
- Based on the
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.
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