Download the PHP package yorcreative/laravel-argonaut-dto without Composer
On this page you can find all versions of the php package yorcreative/laravel-argonaut-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yorcreative/laravel-argonaut-dto
More information about yorcreative/laravel-argonaut-dto
Files in yorcreative/laravel-argonaut-dto
Package laravel-argonaut-dto
Short Description Argonaut is a lightweight Data Transfer Object (DTO) package for Laravel that supports nested casting, recursive serialization, and validation out of the box. Ideal for service layers, APIs, and clean architecture workflows.
License MIT
Informations about the package laravel-argonaut-dto
Laravel Argonaut DTO
Laravel Argonaut DTO is a lightweight, highly composable package for transforming arrays, objects, or collections into structured DTOs (Data Transfer Objects), with built-in support for:
- π§± Deep nested transformation and casting
- π Type-safe data conversion
- β Validation using Laravelβs validator
- π§ Explicit attribute prioritization
- π¦ Clean serialization (
toArray
,toJson
) - β»οΈ Consistent data shape enforcement across boundaries
π¦ Installation
Install via Composer:
π Quick Start
1. Define a DTO
DTOs extend ArgonautDTO
, and define your expected structure via public properties, casting rules, and validation.
This defines a strongly typed DTO with both validation rules and simple type casting.
2. Create an Assembler
Assemblers are responsible for mapping raw inputs (arrays or objects) into your DTOs.
Assembler method names must follow the format
to<ClassName>
, and are resolved automatically usingclass_basename
.
3. Assemble a DTO
Use the assembler to transform raw data into structured, casted DTO instances.
You can also batch transform arrays or collections:
π§ͺ Real-World Example: Product + Features + Reviews
This example demonstrates nested relationships and complex type casting in action.
ProductDTO with nested casting:
ProductDTOAssembler mapping input structure:
π― DTOs with Prioritized Attributes and Custom Setters
ArgonautDTO allows you to prioritize the assignment of specific fields using $prioritizedAttributes
, which is critical for cases where one field influences others.
π Casting Reference
Casting allows you to automatically transform values into other DTOs, Laravel Collections, arrays, dates, and more.
Cast Type | Example | Description |
---|---|---|
Scalar | 'string' , 'int' , etc. |
Native PHP type cast |
Single DTO | ProfileDTO::class |
Cast an array to a DTO instance |
Array of DTOs | [RoleDTO::class] |
Cast to array of DTOs |
Collection of DTOs | Collection::class . ':' . CommentDTO::class |
Cast to a Laravel Collection |
Date casting | Carbon::class |
Cast to Carbon/DateTime instance |
β Validation
Validate DTOs with Laravelβs validator:
π€ Serialization
Serialize DTOs for output, API responses, etc.
π οΈ DTO Collection Helper
Create DTO collections directly:
π§ͺ Testing
Run the test suite using:
π Credits
- Yorda
- All Contributors
π License
This package is open-sourced software licensed under the MIT license.