Top Related Projects
The enum PHP is missing, inspired from SplEnum
Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
Doctrine Object Relational Mapper (ORM)
:card_index_dividers: A PHP library for representing and manipulating collections.
Quick Overview
Spatie's Data Transfer Object (DTO) is a PHP library that provides a convenient way to create strongly-typed data objects. It allows developers to define the structure of data objects, validate their properties, and ensure type safety when working with complex data structures.
Pros
- Improves code readability and maintainability by enforcing strict typing
- Provides built-in validation and casting mechanisms
- Supports nested DTOs and collections
- Offers flexible configuration options for property handling
Cons
- Requires additional setup and boilerplate code compared to using plain arrays
- May introduce a slight performance overhead due to object creation and validation
- Learning curve for developers unfamiliar with the DTO pattern
- Limited flexibility in some scenarios compared to more dynamic data structures
Code Examples
Creating a basic DTO:
use Spatie\DataTransferObject\DataTransferObject;
class UserData extends DataTransferObject
{
public string $name;
public int $age;
public ?string $email;
}
$userData = new UserData([
'name' => 'John Doe',
'age' => 30,
'email' => 'john@example.com',
]);
Using nested DTOs:
class AddressData extends DataTransferObject
{
public string $street;
public string $city;
public string $country;
}
class UserData extends DataTransferObject
{
public string $name;
public AddressData $address;
}
$userData = new UserData([
'name' => 'Jane Doe',
'address' => [
'street' => '123 Main St',
'city' => 'New York',
'country' => 'USA',
],
]);
Working with collections:
use Spatie\DataTransferObject\DataTransferObjectCollection;
class UserCollection extends DataTransferObjectCollection
{
public function current(): UserData
{
return parent::current();
}
}
$users = new UserCollection([
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25],
]);
foreach ($users as $user) {
echo $user->name . ' is ' . $user->age . ' years old.';
}
Getting Started
-
Install the package via Composer:
composer require spatie/data-transfer-object
-
Create your DTO class by extending
DataTransferObject
:use Spatie\DataTransferObject\DataTransferObject; class MyData extends DataTransferObject { public string $property1; public int $property2; }
-
Use your DTO in your code:
$data = new MyData([ 'property1' => 'value1', 'property2' => 42, ]);
Competitor Comparisons
The enum PHP is missing, inspired from SplEnum
Pros of php-enum
- Provides a more type-safe and self-documenting way to define and use enumerations
- Offers built-in methods for comparing and converting enum values
- Lightweight and focused solely on enum functionality
Cons of php-enum
- Limited to enum functionality, lacking the broader data structure capabilities of data-transfer-object
- Requires PHP 5.3 or higher, which may be a constraint for some older projects
- Less flexibility in defining complex data structures compared to data-transfer-object
Code Comparison
php-enum:
class UserType extends Enum
{
const ADMIN = 'admin';
const MODERATOR = 'moderator';
const USER = 'user';
}
$userType = UserType::ADMIN();
data-transfer-object:
class UserData extends DataTransferObject
{
public string $name;
public string $email;
public UserType $type;
}
$userData = new UserData([
'name' => 'John Doe',
'email' => 'john@example.com',
'type' => UserType::ADMIN(),
]);
The php-enum library focuses on creating and managing enum types, while data-transfer-object provides a more comprehensive solution for defining and validating complex data structures, including the ability to use enums as properties.
Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
Pros of Symfony Serializer
- More comprehensive serialization/deserialization capabilities
- Supports multiple formats (JSON, XML, YAML, etc.)
- Integrates well with other Symfony components
Cons of Symfony Serializer
- Steeper learning curve due to more complex API
- Heavier and may introduce unnecessary dependencies for simpler use cases
Code Comparison
Symfony Serializer:
$serializer = new Serializer([new ObjectNormalizer()]);
$data = $serializer->normalize($object);
$json = $serializer->serialize($object, 'json');
Data Transfer Object:
$dto = new MyDTO(['property' => 'value']);
$array = $dto->toArray();
$json = json_encode($dto);
Summary
Symfony Serializer offers more advanced features and flexibility, making it suitable for complex serialization needs and projects already using Symfony components. Data Transfer Object provides a simpler, lightweight solution focused on type-safe data transfer between layers. Choose based on your project's complexity and existing ecosystem.
Doctrine Object Relational Mapper (ORM)
Pros of Doctrine ORM
- Full-featured ORM with advanced querying capabilities and database abstraction
- Supports complex relationships and inheritance mapping
- Extensive ecosystem and integration with popular PHP frameworks
Cons of Doctrine ORM
- Steeper learning curve due to its complexity and extensive features
- Heavier performance overhead, especially for simpler database operations
- Requires more configuration and setup compared to lightweight alternatives
Code Comparison
Data Transfer Object (spatie/data-transfer-object):
class UserData extends DataTransferObject
{
public string $name;
public int $age;
}
Doctrine ORM:
/**
* @Entity
*/
class User
{
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
/** @Column(type="string") */
private $name;
/** @Column(type="integer") */
private $age;
}
Summary
Data Transfer Object is a lightweight library focused on creating simple, type-safe objects for data transfer. It's easy to use and has minimal overhead. Doctrine ORM, on the other hand, is a comprehensive ORM solution with powerful features for complex database operations and object-relational mapping. While Doctrine ORM offers more functionality, it comes with increased complexity and performance considerations.
:card_index_dividers: A PHP library for representing and manipulating collections.
Pros of Collection
- Provides a wide range of collection manipulation methods (map, filter, reduce, etc.)
- Supports type-hinting for better IDE integration and code completion
- Offers immutable collections for safer data handling
Cons of Collection
- Primarily focused on collections, not individual data objects
- May have a steeper learning curve due to its extensive API
- Potentially higher memory usage for large collections
Code Comparison
Collection:
use Ramsey\Collection\Collection;
$collection = new Collection('string');
$collection->add('foo');
$collection->add('bar');
$filtered = $collection->filter(fn($item) => strlen($item) > 2);
Data Transfer Object:
use Spatie\DataTransferObject\DataTransferObject;
class UserData extends DataTransferObject
{
public string $name;
public int $age;
}
$userData = new UserData(['name' => 'John', 'age' => 30]);
Summary
Collection is more suited for working with arrays and collections of data, offering powerful manipulation methods. Data Transfer Object is designed for creating structured, type-safe objects representing individual data entities. Choose Collection for complex array operations and Data Transfer Object for defining clear data structures with validation.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Warning We have decided to stop maintaining this package.
Consider migrating to spatie/laravel-data or cuyz/valinor.
Feel free to fork our code and adapt it to your needs.
Data transfer objects with batteries included
Installation
You can install the package via composer:
composer require spatie/data-transfer-object
- Note: v3 of this package only supports
php:^8.0
. If you're looking for the older version, check out v2.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Usage
The goal of this package is to make constructing objects from arrays of (serialized) data as easy as possible. Here's what a DTO looks like:
use Spatie\DataTransferObject\Attributes\MapFrom;
use Spatie\DataTransferObject\DataTransferObject;
class MyDTO extends DataTransferObject
{
public OtherDTO $otherDTO;
public OtherDTOCollection $collection;
#[CastWith(ComplexObjectCaster::class)]
public ComplexObject $complexObject;
public ComplexObjectWithCast $complexObjectWithCast;
#[NumberBetween(1, 100)]
public int $a;
#[MapFrom('address.city')]
public string $city;
}
You could construct this DTO like so:
$dto = new MyDTO(
a: 5,
collection: [
['id' => 1],
['id' => 2],
['id' => 3],
],
complexObject: [
'name' => 'test',
],
complexObjectWithCast: [
'name' => 'test',
],
otherDTO: ['id' => 5],
);
Let's discuss all possibilities one by one.
Named arguments
Constructing a DTO can be done with named arguments. It's also possible to still use the old array notation. This example is equivalent to the one above.
$dto = new MyDTO([
'a' => 5,
'collection' => [
['id' => 1],
['id' => 2],
['id' => 3],
],
'complexObject' => [
'name' => 'test',
],
'complexObjectWithCast' => [
'name' => 'test',
],
'otherDTO' => ['id' => 5],
]);
Value casts
If a DTO has a property that is another DTO or a DTO collection, the package will take care of automatically casting arrays of data to those DTOs:
$dto = new MyDTO(
collection: [ // This will become an object of class OtherDTOCollection
['id' => 1],
['id' => 2], // Each item will be an instance of OtherDTO
['id' => 3],
],
otherDTO: ['id' => 5], // This data will be cast to OtherDTO
);
Custom casters
You can build your own caster classes, which will take whatever input they are given, and will cast that input to the desired result.
Take a look at the ComplexObject
:
class ComplexObject
{
public string $name;
}
And its caster ComplexObjectCaster
:
use Spatie\DataTransferObject\Caster;
class ComplexObjectCaster implements Caster
{
/**
* @param array|mixed $value
*
* @return mixed
*/
public function cast(mixed $value): ComplexObject
{
return new ComplexObject(
name: $value['name']
);
}
}
Class-specific casters
Instead of specifying which caster should be used for each property, you can also define that caster on the target class itself:
class MyDTO extends DataTransferObject
{
public ComplexObjectWithCast $complexObjectWithCast;
}
#[CastWith(ComplexObjectWithCastCaster::class)]
class ComplexObjectWithCast
{
public string $name;
}
Default casters
It's possible to define default casters on a DTO class itself. These casters will be used whenever a property with a given type is encountered within the DTO class.
#[
DefaultCast(DateTimeImmutable::class, DateTimeImmutableCaster::class),
DefaultCast(MyEnum::class, EnumCaster::class),
]
abstract class BaseDataTransferObject extends DataTransferObject
{
public MyEnum $status; // EnumCaster will be used
public DateTimeImmutable $date; // DateTimeImmutableCaster will be used
}
Using custom caster arguments
Any caster can be passed custom arguments, the built-in ArrayCaster
implementation is a good example of how this may be used.
Using named arguments when passing input to your caster will help make your code more clear, but they are not required.
For example:
/** @var \Spatie\DataTransferObject\Tests\Foo[] */
#[CastWith(ArrayCaster::class, itemType: Foo::class)]
public array $collectionWithNamedArguments;
/** @var \Spatie\DataTransferObject\Tests\Foo[] */
#[CastWith(ArrayCaster::class, Foo::class)]
public array $collectionWithoutNamedArguments;
Note that the first argument passed to the caster constructor is always the array with type(s) of the value being casted.
All other arguments will be the ones passed as extra arguments in the CastWith
attribute.
Validation
This package doesn't offer any specific validation functionality, but it does give you a way to build your own validation attributes. For example, NumberBetween
is a user-implemented validation attribute:
class MyDTO extends DataTransferObject
{
#[NumberBetween(1, 100)]
public int $a;
}
It works like this under the hood:
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
class NumberBetween implements Validator
{
public function __construct(
private int $min,
private int $max
) {
}
public function validate(mixed $value): ValidationResult
{
if ($value < $this->min) {
return ValidationResult::invalid("Value should be greater than or equal to {$this->min}");
}
if ($value > $this->max) {
return ValidationResult::invalid("Value should be less than or equal to {$this->max}");
}
return ValidationResult::valid();
}
}
Mapping
You can map a DTO property from a source property with a different name using the #[MapFrom]
attribute.
It works with a "dot" notation property name or an index.
class PostDTO extends DataTransferObject
{
#[MapFrom('postTitle')]
public string $title;
#[MapFrom('user.name')]
public string $author;
}
$dto = new PostDTO([
'postTitle' => 'Hello world',
'user' => [
'name' => 'John Doe'
]
]);
class UserDTO extends DataTransferObject
{
#[MapFrom(0)]
public string $firstName;
#[MapFrom(1)]
public string $lastName;
}
$dto = new UserDTO(['John', 'Doe']);
Sometimes you also want to map them during the transformation to Array.
A typical usecase would be transformation from camel case to snake case.
For that you can use the #[MapTo]
attribute.
class UserDTO extends DataTransferObject
{
#[MapFrom(0)]
#[MapTo('first_name')]
public string $firstName;
#[MapFrom(1)]
#[MapTo('last_name')]
public string $lastName;
}
$dto = new UserDTO(['John', 'Doe']);
$dto->toArray() // ['first_name' => 'John', 'last_name'=> 'Doe'];
$dto->only('first_name')->toArray() // ['first_name' => 'John'];
Strict DTOs
The previous version of this package added the FlexibleDataTransferObject
class which allowed you to ignore properties that didn't exist on the DTO. This behaviour has been changed, all DTOs are flexible now by default, but you can make them strict by using the #[Strict]
attribute:
class NonStrictDto extends DataTransferObject
{
public string $name;
}
// This works
new NonStrictDto(
name: 'name',
unknown: 'unknown'
);
use \Spatie\DataTransferObject\Attributes\Strict;
#[Strict]
class StrictDto extends DataTransferObject
{
public string $name;
}
// This throws a \Spatie\DataTransferObject\Exceptions\UnknownProperties exception
new StrictDto(
name: 'name',
unknown: 'unknown'
);
Helper functions
There are also some helper functions provided for working with multiple properties at once.
$postData->all();
$postData
->only('title', 'body')
->toArray();
$postData
->except('author')
->toArray();
Note that all()
will simply return all properties, while toArray()
will cast nested DTOs to arrays as well.
You can chain the except()
and only()
methods:
$postData
->except('title')
->except('body')
->toArray();
It's important to note that except()
and only()
are immutable, they won't change the original data transfer object.
Immutable DTOs and cloning
This package doesn't force immutable objects since PHP doesn't support them, but you're always encouraged to keep your DTOs immutable. To help you, there's a clone
method on every DTO which accepts data to override:
$clone = $original->clone(other: ['name' => 'a']);
Note that no data in $original
is changed.
Collections of DTOs
This version removes the DataTransferObjectCollection
class. Instead you can use simple casters and your own collection classes.
Here's an example of casting a collection of DTOs to an array of DTOs:
class Bar extends DataTransferObject
{
/** @var \Spatie\DataTransferObject\Tests\Foo[] */
#[CastWith(FooArrayCaster::class)]
public array $collectionOfFoo;
}
class Foo extends DataTransferObject
{
public string $name;
}
class FooArrayCaster implements Caster
{
public function cast(mixed $value): array
{
if (! is_array($value)) {
throw new Exception("Can only cast arrays to Foo");
}
return array_map(
fn (array $data) => new Foo(...$data),
$value
);
}
}
If you don't want the redundant typehint, or want extended collection functionality; you could create your own collection classes using any collection implementation. In this example, we use Laravel's:
class Bar extends DataTransferObject
{
#[CastWith(FooCollectionCaster::class)]
public CollectionOfFoo $collectionOfFoo;
}
class Foo extends DataTransferObject
{
public string $name;
}
use Illuminate\Support\Collection;
class CollectionOfFoo extends Collection
{
// Add the correct return type here for static analyzers to know which type of array this is
public function offsetGet($key): Foo
{
return parent::offsetGet($key);
}
}
class FooCollectionCaster implements Caster
{
public function cast(mixed $value): CollectionOfFoo
{
return new CollectionOfFoo(array_map(
fn (array $data) => new Foo(...$data),
$value
));
}
}
Simple arrays of DTOs
For a simple array of DTOs, or an object that implements PHP's built-in ArrayAccess
, consider using the ArrayCaster
which requires an item type to be provided:
class Bar extends DataTransferObject
{
/** @var \Spatie\DataTransferObject\Tests\Foo[] */
#[CastWith(ArrayCaster::class, itemType: Foo::class)]
public array $collectionOfFoo;
}
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
External tools
- json2dto: a GUI to convert JSON objects to DTO classes (with nesting support). Also provides a CLI tool for local usage.
- Data Transfer Object Factory: Intelligently generates a DTO instance using the correct content for your properties based on its name and type.
Credits
Our Arr
class contains functions copied from Laravels Arr
helper.
License
The MIT License (MIT). Please see License File for more information.
Top Related Projects
The enum PHP is missing, inspired from SplEnum
Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
Doctrine Object Relational Mapper (ORM)
:card_index_dividers: A PHP library for representing and manipulating collections.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot