Download the PHP package olexin-pro/data-transfer-object without Composer
On this page you can find all versions of the php package olexin-pro/data-transfer-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download olexin-pro/data-transfer-object
More information about olexin-pro/data-transfer-object
Files in olexin-pro/data-transfer-object
Informations about the package data-transfer-object
๐ Data Transfer Object
A powerful, strict, and production-ready DTO layer for PHP/Laravel.
This package provides automatic type conversion, PHP 8 Attribute mapping, nested DTO support, Laravel Casts, and seamless integration with API Resources. It ensures your data is validated, normalized, and strongly typed from the moment it enters your application.
โจ Features
- ๐ Auto-Conversion: Automatically transforms input arrays/JSON into strongly typed objects.
- ๐ PHP 8 Attributes: Declarative field mapping with
#[Field]. - ๐ Enum Casting: Strict type enforcement using native Enums.
- ๐งฉ Nested DTOs: Recursively resolves complex data structures.
- โ๏ธ Strict Validation: Throws errors on type mismatches or missing required fields.
- ๐ฆ Laravel Integration: Includes Model Casts and Form Request mapping.
- ๐ Resource Pipeline: Request โ DTO โ API Resource flow.
- ๐ช High Performance: Uses reflection caching to minimize overhead.
Table of Contents
- Installation
- Quick Start
- Usage Guide
- Manual Instantiation
- Nested DTOs
- Laravel Integration
- Request Injection
- Model Casts
- API Resources
- Reference
- Supported Types
- Testing
- License
๐ฆ Installation
Requires PHP 8.3+
`
๐ Quick Start
1. Define your DTO
Create a class extending AbstractDTO and use attributes to define fields.
2. Use it
๐ฏ Usage Guide
Manual Instantiation
You can instantiate a DTO with any iterable (array or collection). Keys are automatically normalized from camelCase to snake_case during processing.
Nested DTOs
Complex structures are handled automatically. Simply typehint the property with another DTO class and use TypeEnum::DTO.
Input:
Result: $dto->profile is an instance of ProfileDTO.
๐ฅ Laravel Integration
Request Injection
The package integrates seamlessly with Laravel's Service Container. You can typehint a DTO in your Controller method, and it will automatically hydrate from the current Request (supporting JSON, Form Data, Query Params).
Alternatively, use fromRequest:
Model Casts
Store DTOs directly in your database using Laravel's Custom Casts. The DTO is serialized to JSON on save and hydrated back to a DTO on retrieval.
In your Model:
Usage:
API Resources
DTOs implement Arrayable, making them compatible with Laravel API Resources.
๐งฐ Reference
Supported Types (TypeEnum)
The #[Field] attribute requires a type definition from TypeEnum.
| Enum Case | Description |
|---|---|
TypeEnum::INT |
Casts to integer |
TypeEnum::FLOAT |
Casts to float |
TypeEnum::STRING |
Casts to string |
TypeEnum::BOOLEAN |
Casts to boolean |
TypeEnum::ARRAY |
Casts to array |
TypeEnum::DATE |
Handles date strings |
TypeEnum::DTO |
Hydrates a nested DTO |
TypeEnum::COLLECTION |
Hydrates a collection of DTOs/items |
TypeEnum::DYNAMIC |
No casting, keeps original type |
๐ง Architecture & Principles
- Immutability: DTOs are designed to be immutable after instantiation.
- Safety: Internal fields (
_raw, etc.) are hidden. - Normalization: Input keys are normalized (snake_case/camelCase handling).
- Performance: Reflection metadata is cached to ensure production speed.
๐งช Testing
Run the full test suite:
๐ License
This package is open-sourced software licensed under the MIT license.
All versions of data-transfer-object with dependencies
illuminate/support Version ^10.0 || ^11.0 || ^12.0
illuminate/contracts Version ^10.0 || ^11.0 || ^12.0
illuminate/collections Version ^10.0 || ^11.0 || ^12.0
illuminate/database Version ^10.0 || ^11.0 || ^12.0