dragon-code/simple-dto
Lightweight PHP DTO helper: define simple DataTransferObject classes, build instances via make(), map nested input keys to properties, and (optionally) cast values. Supports upgrade paths from older package names. Note: author recommends spatie/laravel-data instead.
Start by installing the package via Composer:
composer require dragon-code/simple-dto
Then define your first DTO by extending DragonCode\SimpleDataTransferObject\DataTransferObject:
use DragonCode\SimpleDataTransferObject\DataTransferObject;
class UserDTO extends DataTransferObject
{
public string $name;
public string $email;
}
Use it immediately in a controller or service:
$requestData = $request->only(['name', 'email']);
$user = UserDTO::make($requestData);
💡 First use case: Clean input validation results — extract validated request data into a DTO to pass into services without worrying about raw arrays.
Type-hinted properties: Use typed properties (public string $email) to catch mismatches early. PHP will enforce types during assignment or casting.
Field mapping: Use the $map array to flatten nested input structures (e.g., from API payloads or JSON):
protected $map = [
'data.user.name' => 'name',
'data.user.email' => 'email',
];
Custom casting with methods: Implement cast{Property}($value) methods for transformations:
protected function castName(string $value): string
{
return mb_strtolower(trim($value));
}
Nested DTOs: Use casting to construct sub-objects:
protected function castAddress(array $address): AddressDTO
{
return AddressDTO::make($address);
}
Flexible creation: Leverage fromRequest(), fromJson(), fromArray(), and fromObject() for seamless integration across layers:
$dto = UserDTO::fromRequest($request);
DTOable interface: For request classes, implement DragonCode\Contracts\DataTransferObject\Dtoable to standardize DTO creation:
class StoreUserRequest implements Dtoable
{
public function dto(): DataTransferObject
{
return UserDTO::fromRequest($this);
}
}
Dynamic access: Use get()/set() for runtime property access and toArray()/toJson() for serialization:
$dto->set('name', 'Jane');
echo $dto->toJson();
Package is archived: As of 2024-03-06, the package is archived — consider migrating to spatie/laravel-data, recommended in the README.
Casts aren’t type enforced at assignment: PHP won’t auto-throw on failed casts unless strict types and typed properties are used. Always rely on typed properties + casts to avoid silent failures.
Missing keys don’t fail by default: Undefined properties default to null unless typed (public string $foo → TypeError). Use ?? or get() for safe access.
$only and $except properties: Available since v2.7.0 — useful for filtering input before mapping:
protected $only = ['name', 'email'];
Boolean pitfalls: v2.2.2 fixed a boolean-processing bug — ensure you’re on ≥v2.2.2 if using boolean fields.
No validation built-in: This is a data transfer object, not a validator. Combine with Laravel’s validation or illuminate/validation.
Namespace migration needed: If upgrading from andrey-helldar/simple-data-transfer-object, double-check namespace replacement to avoid runtime errors.
Laravel integration: For requests, consider coupling with form requests:
public function rules(): array { return [...]; }
public function dto(): DataTransferObject { return UserDTO::fromRequest($this); }
Debugging tip: Use dd($dto->toArray()) or dd($dto->jsonSerialize()) to inspect final structure before use.
How can I help you explore Laravel packages today?