Download the PHP package sinmiloluwa/laravel-dto-mapper without Composer
On this page you can find all versions of the php package sinmiloluwa/laravel-dto-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sinmiloluwa/laravel-dto-mapper
More information about sinmiloluwa/laravel-dto-mapper
Files in sinmiloluwa/laravel-dto-mapper
Package laravel-dto-mapper
Short Description Elegant, type-safe DTO mapping for Laravel models and arrays.
License MIT
Informations about the package laravel-dto-mapper
Laravel DTO Mapper
๐งฉ Elegant, type-safe DTO mapping for Laravel models and arrays.
๐ Features
- โ Map Eloquent models or arrays to typed DTOs
- ๐ฏ Supports PHP 8+ attributes (
#[MapFrom],#[Cast]) - ๐ง Automatic type casting (
int,bool,float,string) - โก Zero configuration โ drop it in and use
- ๐ก๏ธ Clear errors for missing or null values instead of silent defaults
๐ฆ Installation
Requires PHP 8.1+ and Laravel 10 or 11.
๐ ๏ธ Usage
Add the MapsAttributes trait to a class with public typed properties, then call from() with an array, an Eloquent model or any object:
Each public, non-static property is filled from the source key with the same name.
#[Cast]
Converts the value before it is assigned. The supported types are int, float, bool and string. A null value is never cast.
#[MapFrom]
Declares which class the DTO is built from. Passing an object of any other class throws a DtoMappingException. Arrays are always accepted.
Missing and null values
| Source value | Property | Result |
|---|---|---|
| key missing | has a default (= 'member') |
default is kept |
key missing or null |
nullable (?string) |
null |
key missing or null |
not nullable | DtoMappingException |
๐งช Testing
๐ License
MIT. See LICENSE.