Download the PHP package hdaklue/laravel-dto-morph-cast without Composer

On this page you can find all versions of the php package hdaklue/laravel-dto-morph-cast. 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-dto-morph-cast

Laravel DTO MorphCast

This package is a plugin for the WendellAdriel/laravel-validated-dto package.

Latest Version on Packagist Tests Total Downloads

A powerful MorphCast plugin for Laravel Validated DTO that provides dynamic polymorphic casting for Eloquent models. This package allows you to dynamically cast data to different Eloquent model instances based on morphable type information within your DTOs.

Features

Installation

You can install the package via Composer:

Requirements

Usage

Basic Usage

The MorphCast class automatically resolves polymorphic relationships in your DTOs. Here's how to use it:

Setting Up Morph Maps

First, define your morph map in a service provider:

DTO Data Structure

Your DTO data should include the morph type key:

Hiding Sensitive Data

You can hide sensitive fields from being cast to the model by passing them as an array to the constructor:

You can also use Laravel's array-based casting syntax:

When sensitive fields are specified, they will be excluded from the forceFill() operation:

Naming Conventions

By default, MorphCast follows Laravel's polymorphic relationship conventions:

Custom Key Resolution

You can extend MorphCast to customize key resolution:

Error Handling

MorphCast provides clear error messages for common issues:

How It Works

  1. Trace Analysis: Uses debug backtrace to access the parent DTO's data array
  2. Key Resolution: Automatically determines morph type keys based on property names
  3. Model Resolution: Uses Laravel's morph map to resolve the actual model class
  4. Validation: Ensures the resolved class exists and extends Eloquent Model
  5. Instantiation: Creates and fills the model instance with the provided data

Performance Considerations

Minimal Debug Backtrace Usage

This package uses debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2) with a strict limit of 2 levels to access the parent DTO's data array. This approach provides several benefits:

The small performance cost of this limited backtrace is offset by the significant flexibility gain of automatic morph type resolution, making polymorphic casting seamless and convention-based.

Testing

Code Style

This package uses Laravel Pint for code formatting:

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

If you discover a security vulnerability, please send an e-mail to [email protected].

Credits

License

The MIT License (MIT). Please see License File for more information.

Related


All versions of laravel-dto-morph-cast with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/database Version ^11.0|^12.0
wendelladriel/laravel-validated-dto Version ^4.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 hdaklue/laravel-dto-morph-cast contains the following files

Loading the files please wait ...