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.
Download hdaklue/laravel-dto-morph-cast
More information about hdaklue/laravel-dto-morph-cast
Files in hdaklue/laravel-dto-morph-cast
Package laravel-dto-morph-cast
Short Description MorphCast for Laravel Validated DTO - Dynamic polymorphic casting for Eloquent models
License MIT
Informations about the package laravel-dto-morph-cast
Laravel DTO MorphCast
This package is a plugin for the WendellAdriel/laravel-validated-dto package.
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
- Dynamic Model Resolution: Automatically resolves model classes using Laravel's morph map
- Polymorphic Casting: Cast data to different model types based on morph type keys
- Sensitive Data Protection: Hide sensitive fields from being cast to model instances
- Seamless Integration: Works perfectly with Laravel Validated DTO package
- Type Safety: Validates model classes and throws meaningful exceptions
- Convention Based: Follows Laravel's morphable naming conventions
Installation
You can install the package via Composer:
Requirements
- PHP 8.2 or higher
- Laravel 11.0 or higher
- Laravel Validated DTO 4.0 or higher
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:
- For a property named
commentable, it looks forcommentable_type - For a property named
imageable, it looks forimageable_type
Custom Key Resolution
You can extend MorphCast to customize key resolution:
Error Handling
MorphCast provides clear error messages for common issues:
How It Works
- Trace Analysis: Uses debug backtrace to access the parent DTO's data array
- Key Resolution: Automatically determines morph type keys based on property names
- Model Resolution: Uses Laravel's morph map to resolve the actual model class
- Validation: Ensures the resolved class exists and extends Eloquent Model
- 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:
- Minimal Overhead: Only traces 2 stack frames (current + parent), not the entire call stack
- Targeted Access: Specifically retrieves only the object data needed for casting
- Auto-Resolution: Eliminates the need for manual configuration or explicit passing of morph type information
- Clean API: Maintains a simple, intuitive interface without complex setup requirements
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
- Hassan Ibrahim
- Built as an extension for Laravel Validated DTO
License
The MIT License (MIT). Please see License File for more information.
Related
- Laravel Validated DTO - The main package this extends
All versions of laravel-dto-morph-cast with dependencies
illuminate/database Version ^11.0|^12.0
wendelladriel/laravel-validated-dto Version ^4.0