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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-argonaut-dto

Laravel Argonaut DTO

GitHub license GitHub stars GitHub Org's stars GitHub issues GitHub forks PHPUnit

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:


πŸ“¦ 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> or from<ClassName>, and are resolved automatically using class_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:

🎯 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:

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.

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:


🎯 Nested Assemblers for Deep Transformations

Nested assemblers enhance the casting process by allowing you to specify an assembler class for individual fields in your DTO. When a value is assigned to that field during construction or attribute setting, the raw input is first passed through the assembler's transformation method (e.g., toUserDTO) before the cast is applied. This is ideal for handling complex, nested data structures where raw inputs need preprocessing or mapping.

Nested assemblers integrate seamlessly with casting:

Defining Nested Assemblers

Add a protected array $nestedAssemblers property to your DTO, mapping field names to assembler classes:

In this example:

How It Works

When setting attributes (via constructor or setAttributes):

  1. If a nested assembler is defined for the key and a cast exists, the assembler transforms the value (or each item for iterables).
  2. The transformed value is then cast according to $casts (e.g., into a DTO, collection, etc.).
  3. Scalars (non-array/object values) skip the assembler to avoid type errors.

This ensures deep, automatic transformations while maintaining type safety and structure.

Example Usage

With the above ProductDTO and a raw input:

Nested assemblers promote composability, making it easier to handle multi-layered data in APIs, services, or complex domain logic.


πŸ”’ Immutable DTOs

For scenarios where data integrity is critical, ArgonautImmutableDTO provides a base class for creating DTOs with readonly properties that cannot be modified after construction.

When to Use Immutable DTOs

Defining an Immutable DTO

Usage

Immutable DTOs with Assemblers

Immutable DTOs work seamlessly with assemblers:

Nested Immutable DTOs

Immutable DTOs support all casting features including nested DTOs:

Comparison: Mutable vs Immutable

Feature ArgonautDTO ArgonautImmutableDTO
Property modification βœ… Allowed ❌ Blocked (readonly)
Custom setters βœ… setPropertyName() ❌ Not supported
setAttributes() / merge() βœ… Available ❌ Not available
Casting (DTOs, enums, dates) βœ… Full support βœ… Full support
Validation βœ… Full support βœ… Full support
Serialization (toArray, toJson, only, except) βœ… Full support βœ… Full support
Assembler integration βœ… Full support βœ… Full support
Nested assemblers βœ… Full support βœ… Full support
PHP requirement 8.2+ 8.2+ (readonly properties)

Note: Choose ArgonautDTO when you need mutable objects with custom setters. Choose ArgonautImmutableDTO when you want guaranteed immutability and don't need post-construction modifications.


🎯 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, enums, and more.

Cast Type Example Description
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
BackedEnum StatusEnum::class Cast a raw value to a BackedEnum
Array of Enums [TagEnum::class] Cast to array of BackedEnum instances
Collection of Enums Collection::class . ':' . PriorityEnum::class Cast to a Collection of BackedEnums

BackedEnum Casting

PHP BackedEnum types are automatically detected and cast using Enum::from(). Existing enum instances are passed through unchanged. On serialization, enums are converted back to their backing value.


βœ… Validation

Validate DTOs with Laravel’s validator:


πŸ“€ Serialization

Serialize DTOs for output, API responses, etc.

Partial Serialization

Use only() and except() to serialize a subset of properties:


πŸ”„ Merging Attributes

Mutable DTOs support merging additional attributes after construction:

merge() returns the same instance, so it can be chained:

Note: merge() is only available on ArgonautDTO. Immutable DTOs do not support post-construction modification.


🧩 Trait-Based Architecture

Under the hood, both ArgonautDTO and ArgonautImmutableDTO compose shared behavior from three traits:

Trait Provides
HasCasting castInputValue(), enum/DTO/collection/date casting
HasSerialization toArray(), toJson(), only(), except(), collection()
HasValidation validate(), isValid()

This is transparent to most users, but if you are extending internal behavior (e.g., overriding castInputValue() in a subclass), note that these methods now live in traits rather than directly on the base class. Method resolution is identical for inheritance purposes.


πŸ› οΈ DTO Collection Helper

Create DTO collections directly:


πŸ§ͺ Testing

Run the test suite:

Run tests with coverage report:

Run static analysis (PHPStan):

Run code style fixer (Pint):


πŸ“š Credits


πŸ“ƒ License

This package is open-sourced software licensed under the MIT license.


All versions of laravel-argonaut-dto with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2|^8.3|^8.4|^8.5
illuminate/contracts Version ^10.0|^11.0|^12.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package yorcreative/laravel-argonaut-dto contains the following files

Loading the files please wait ...