PHP code example of philiprehberger / php-dto-mapper

1. Go to this page and download the library: Download philiprehberger/php-dto-mapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

philiprehberger / php-dto-mapper example snippets


use PhilipRehberger\DtoMapper\Attributes\MapFrom;
use PhilipRehberger\DtoMapper\Attributes\Optional;
use PhilipRehberger\DtoMapper\Attributes\CastWith;
use PhilipRehberger\DtoMapper\Casters\DateTimeCaster;

class UserDto
{
    public function __construct(
        public readonly string $name,
        #[MapFrom('email_address')]
        public readonly string $email,
        #[Optional]
        public readonly ?string $nickname = null,
        #[CastWith(DateTimeCaster::class)]
        public readonly ?\DateTimeImmutable $createdAt = null,
    ) {}
}

use PhilipRehberger\DtoMapper\DtoMapper;

$dto = DtoMapper::map([
    'name' => 'John',
    'email_address' => '[email protected]',
    'createdAt' => '2026-01-15 10:30:00',
], UserDto::class);

$dto->name;      // 'John'
$dto->email;     // '[email protected]'
$dto->createdAt; // DateTimeImmutable

$dto = DtoMapper::mapJson('{"name": "Jane", "email_address": "[email protected]"}', UserDto::class);

$dtos = DtoMapper::mapCollection([
    ['name' => 'Alice', 'email_address' => '[email protected]'],
    ['name' => 'Bob', 'email_address' => '[email protected]'],
], UserDto::class);

$dto = DtoMapper::tryMap($data, UserDto::class); // Returns null on failure

$dto = DtoMapper::strict([
    'name' => 'John',
    'email_address' => '[email protected]',
], UserDto::class);

// Throws MappingException: Unknown field "extra_key"
DtoMapper::strict([
    'name' => 'John',
    'email_address' => '[email protected]',
    'extra_key' => 'oops',
], UserDto::class);

class AddressDto
{
    public function __construct(
        public readonly string $street,
        public readonly string $city,
    ) {}
}

class PersonDto
{
    public function __construct(
        public readonly string $name,
        public readonly AddressDto $address,
    ) {}
}

$dto = DtoMapper::map([
    'name' => 'Alice',
    'address' => ['street' => '123 Main St', 'city' => 'Springfield'],
], PersonDto::class);

$dto->address->city; // 'Springfield'

class ProfileDto
{
    public function __construct(
        public readonly string $name,
        #[MapFrom('user.profile.email')]
        public readonly string $email,
    ) {}
}

$dto = DtoMapper::map([
    'name' => 'Alice',
    'user' => [
        'profile' => ['email' => '[email protected]'],
    ],
], ProfileDto::class);

$dto->email; // '[email protected]'

class ProfileDto
{
    public function __construct(
        public readonly string $name,
        public readonly int $age,
        public readonly ?string $bio = null,
        public readonly string $role = 'member',
    ) {}
}

$dto = DtoMapper::mapPartial([
    'name' => 'Alice',
], ProfileDto::class);

$dto->name; // 'Alice'
$dto->bio;  // null (nullable gets null)
$dto->role; // 'member' (default preserved)
// $dto->age is not set (non-nullable without default, skipped)

class EventDto
{
    public function __construct(
        public readonly string $name,
        public readonly int|string $identifier,
    ) {}
}

$dto = DtoMapper::map(['name' => 'Login', 'identifier' => '99'], EventDto::class);
$dto->identifier; // 99 (coerced to int, the first type)

$dto = DtoMapper::map(['name' => 'Login', 'identifier' => 'abc-123'], EventDto::class);
$dto->identifier; // 'abc-123' (kept as string)

use PhilipRehberger\DtoMapper\Contracts\Caster;

class MoneyFromCentsCaster implements Caster
{
    public function cast(mixed $value): float
    {
        return (int) $value / 100;
    }
}

class OrderDto
{
    public function __construct(
        #[CastWith(MoneyFromCentsCaster::class)]
        public readonly float $total,
    ) {}
}

use PhilipRehberger\DtoMapper\Attributes\CastWith;
use PhilipRehberger\DtoMapper\Casters\CollectionCaster;

class ItemDto
{
    public function __construct(
        public readonly string $name,
        public readonly int $quantity,
    ) {}
}

class OrderDto
{
    public function __construct(
        public readonly string $orderId,
        #[CastWith(CollectionCaster::class, args: [ItemDto::class])]
        public readonly array $items,
    ) {}
}

$dto = DtoMapper::map([
    'orderId' => 'ORD-001',
    'items' => [
        ['name' => 'Widget', 'quantity' => 3],
        ['name' => 'Gadget', 'quantity' => 1],
    ],
], OrderDto::class);

$dto->items[0]->name; // 'Widget'

use PhilipRehberger\DtoMapper\Attributes\CastWith;
use PhilipRehberger\DtoMapper\Casters\EnumCaster;

enum Status: string
{
    case Active = 'active';
    case Inactive = 'inactive';
}

class AccountDto
{
    public function __construct(
        public readonly string $name,
        #[CastWith(EnumCaster::class, args: [Status::class])]
        public readonly Status $status,
    ) {}
}
bash
composer