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>
orfrom<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 Static Usage Example: Product + Features + Reviews
This example demonstrates nested relationships and complex type casting in action.
ProductDTO with nested casting:
ProductDTOAssembler mapping input structure:
🎯 Advanced: Dependency Injection in Assemblers
ArgonautAssembler offers enhanced flexibility for your Assembler logic by supporting dependency injection. This allows you to leverage services or custom logic, whether defined in static or non-static methods, during the DTO assembly process. This is particularly powerful when integrating with Laravel's service container.
This feature enables you to:
- Integrate Application Services: Easily inject your existing application services (e.g., a custom formatting utility, a validation service) directly into your assembler methods.
- Decouple Complex Logic: Keep your assembler methods focused on the core task of data mapping by delegating more complex operations or external data fetching/processing to injected dependencies.
- Improve Testability: By injecting dependencies, you can more easily mock them in your unit tests, leading to more robust and isolated tests for your assemblers.
How Dependency Injection Works
ArgonautAssembler
supports dependency injection in non-static transformation methods (e.g., toUserDTO
or
fromUserDTO
) by leveraging Laravel’s service container. When you call ArgonautAssembler::assemble()
,
fromCollection()
, fromArray()
, or assembleInstance()
with an instance of the assembler, the transformation method
is invoked on that instance. Laravel’s container automatically resolves and injects any dependencies declared in the
method’s signature.
- Static Methods: Static transformation methods (e.g.,
public static function toUserDTO($input)
) do not support dependency injection, as they are called statically without an instance. - Instance Methods: Non-static transformation methods (e.g.,
public function toUserDTO($input)
) are called on an assembler instance, allowing Laravel to inject dependencies into the method.
Example: Using Dependency Injection
Below is an example of an assembler with a non-static transformation method that uses dependency injection to format a user’s name via an injected service.
Registering the Assembler
Using the Assembler
To use the assembler with dependency injection, you need to provide an instance of the assembler to the assemble
method or related methods (fromCollection
, fromArray
, or assembleInstance
). Laravel’s container will resolve the
dependencies when the method is invoked.
In this example:
- The
toUserDTO
method requires aUserFormattingService
dependency. - The assembler instance (
$assembler
) is passed toassemble
,fromArray
orfromCollection
, ensuring the non-statictoUserDTO
method is invoked on the instance.
Advanced: 🎯 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.