Hydrating DTOs
The from
method allows you to create DTO instances from arrays, JSON or objects.
Basic Usage
Use the static from
method to hydrate the DTO:
php
// From an array
$user = User::from([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
]);
// From a JSON string
$user = User::from('{"name": "John Doe", "email": "john.doe@example.com"}');
// From Objects like frameworks like Phalcon or Laravel
$userModel = UserModel::findFirst(1);
$user = User::from($userModel);
Nested DTOs
Carapace automatically handles nested DTOs:
php
class Address extends Data
{
public function __construct(
public string $street,
public string $city,
public string $zipCode,
) {}
}
class User extends Data
{
public function __construct(
public string $name,
public string $email,
#[CastWith(Address::class)]
public Address $address,
) {}
}
$user = User::from([
'name' => 'John Doe',
'email' => 'john@example.com',
'address' => [
'street' => '123 Main St',
'city' => 'Anytown',
'zipCode' => '12345',
],
]);
echo $user->address->city; // Outputs: Anytown
Collections of DTOs
You can also work with collections of DTOs:
php
class Team extends Data
{
public function __construct(
public string $name,
#[CastWith(User::class)]
/** @var User[] */
public array $members,
) {}
}
$team = Team::from([
'name' => 'Engineering',
'members' => [
['name' => 'John', 'email' => 'john@example.com'],
['name' => 'Jane', 'email' => 'jane@example.com'],
],
]);
echo $team->members[0]->name; // Outputs: John
IMPORTANT
The @var
is to help IDEs understand the type of the members
property. Carapace will automatically cast each using theCastWith
item in the array to the specified DTO type.