nandan108/dto-toolkit

A modern, framework-agnostic PHP 8.1+ DTO system with attribute-driven data normalization and mapping to entities or arrays.

v0.6.0 2025-05-20 16:00 UTC

This package is auto-updated.

Last update: 2025-06-20 16:12:21 UTC


README

CI Coverage Style Packagist

✅ 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

🧩 Adapter Packages

Adapters will provide support for:

  • Validation using framework services
  • fromRequest() DTO hydration
  • toEntity() or toModel() adapter-specific hydration
  • toResponse() 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