mohammedmanssour / super-simple-dto
Creating data transfer objects with the power of php objects
Fund package maintenance!
mohammedmanssour
Installs: 1 651
Dependents: 1
Suggesters: 0
Security: 0
Stars: 12
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- php: ^8.1
- laravel/framework: ^8.0|^9.0|^10.0|^11.1|^12.0
Requires (Dev)
- laravel/pint: ^1.2
- mockery/mockery: ^1.5
- nunomaduro/collision: ^8.8
- phpunit/phpunit: ^12.2.7
- spatie/ray: ^1.28
This package is auto-updated.
Last update: 2025-07-18 13:35:41 UTC
README
Creating data transfer objects with the power of php objects. Simple, lightweight, and efficient DTO conversion with automatic type handling.
This is a laravel package and laravel/framework
is part of the package dependencies. Please, make sure you have no problem with that before using.
Why Bother.
The spatie team has already created an awesome package that serves as a great solution for DTO objects. But, for me, it's full of features that I don't use and it seems like an overkill for me when I just wanted simple solution for DTO work.
Installation
You can install the package via composer:
composer require mohammedmanssour/super-simple-dto
Usage
- Apply the
AsDTO
trait to your data object
use MohammedManssour\DTO\Concerns\AsDTO; class UserData { use AsDTO; public string $name; public string $email; public BalanceData $balance; public Status $status; }
- Use one of these static methods to convert data into DTO:
fromCollection
: converts collections to DTO objects.fromArray
: converts array to DTO objects.fromModel
: converts model to DTO objects. It works with the data available with$model->getAttributes()
method.fromRequest
: converts laravel requests to DTO objects. It works with the data available withvalidated()
. In casevalidated
method is not available, it'll use theall()
method. You can also force using request'sall
method by passing true as a second parameter.
UserData::fromCollection(collect([])); UserData::fromArray([]); UserData::fromModel($model); UserData::fromRequest($request);
Features
Automatic Type Conversion
The package automatically handles type conversion for:
- Enums: Automatically converts values to enum instances
- DTOs: Automatically converts arrays/objects to other DTO instances
- Built-in types: Handles all PHP built-in types
Array to DTO Collection Mapping with MapInto
The MapInto
attribute allows you to automatically convert arrays of data into arrays of DTO objects. This is particularly useful when working with collections of related data.
use MohammedManssour\DTO\Concerns\AsDTO; use MohammedManssour\DTO\Support\MapInto; class WalletData { use AsDTO; public string $type; public int $balance; } class UserData { use AsDTO; public string $name; public string $email; #[MapInto(WalletData::class)] public array $wallets; } $data = [ 'name' => 'Mohammed Manssour', 'email' => 'hello@mohammedmanssour.me', 'wallets' => [ ['type' => 'bitcoin', 'balance' => 1000], ['type' => 'ethereum', 'balance' => 500], ] ]; $user = UserData::fromArray($data); // $user->wallets will contain an array of WalletData objects foreach ($user->wallets as $wallet) { echo $wallet->type . ': ' . $wallet->balance; // Each wallet is a WalletData instance }
Custom Property Setters
You can define custom setter methods for properties that need special handling:
class UserData { use AsDTO; public Carbon $created_at; public function setCreatedAt($value) { $this->created_at = Carbon::parse($value); } }
Type Safety
The package respects PHP type declarations and automatically converts:
class BalanceData { use AsDTO; public float $bitcoin; public int $usdollar; } enum Status: string { case Active = 'active'; case Suspended = 'suspended'; } class UserData { use AsDTO; public BalanceData $balance; // Automatically converted from array public Status $status; // Automatically converted from string } $data = [ 'balance' => [ 'bitcoin' => 10.5, 'usdollar' => 1000 ], 'status' => 'active' ]; $dto = UserData::fromArray($data); // $dto->balance is a BalanceData instance // $dto->status is a Status enum instance
Property Initialization Check
The package provides a helpful isset()
method that checks if a property was actually initialized (different from PHP's native isset()
which returns false for null
values):
$dto = UserData::fromArray([ 'name' => 'Mohammed', 'email' => null // explicitly set to null ]); // Native PHP isset isset($dto->name); // true isset($dto->email); // false (because it's null) isset($dto->balance); // false (not initialized) // DTO isset method $dto->isset('name'); // true $dto->isset('email'); // true (was explicitly set, even though null) $dto->isset('balance'); // false (not initialized)
Converting DTO to Array
You can convert any DTO back to an array:
$dto = UserData::fromArray($data); $array = $dto->toArray();
The toArray()
method handles:
- Nested DTOs (converts them to arrays recursively)
- Enums (converts to their scalar values)
- Carbon instances (keeps as Carbon objects)
- Arrays of DTOs (converts each DTO to array)
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.