PHPackages                             sj/dtomatic - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. sj/dtomatic

ActiveLibrary[Database &amp; ORM](/categories/database)

sj/dtomatic
===========

Dtomatic is a flexible, reflection-based object mapper for Laravel applications. It automatically maps data from source objects to DTO classes, including support for nested objects and collections

v1.0.2(9mo ago)015MITPHPPHP ^8.1

Since Jun 22Pushed 9mo agoCompare

[ Source](https://github.com/shihab-jamil/dtomatic)[ Packagist](https://packagist.org/packages/sj/dtomatic)[ RSS](/packages/sj-dtomatic/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)DependenciesVersions (4)Used By (0)

📦 Dtomatic
==========

[](#-dtomatic)

**Dtomatic** is a flexible, reflection-based object-to-DTO mapper for Laravel applications. It automatically maps data from source models or plain objects to strongly typed DTO classes — including support for:

- Nested DTO mapping
- Collections
- Custom value converters
- Ignoring specific properties
- Strict type validation
- Mapping values via custom model getters
- Clean integration with Laravel services

---

📥 Installation
--------------

[](#-installation)

Install the package via Composer:

```
composer require sj/dtomatic
```

---

🔧 Configuration
---------------

[](#-configuration)

To publish the configuration file:

```
php artisan vendor:publish --provider="Dtomatic\DtomaticServiceProvider" --tag=dtomatic-config
```

This will publish a config file at:

```
config/dtomatic.php

```

Example content:

```
return [

    'strict_types' => true, //If enabled, Dtomatic will throw an exception when a type mismatch occurs.
    'date_format' => 'Y-m-d H:i:s', // The default date format used when converting DateTime objects to strings

    //You can register custom type converters here. When mapping a value,
    //Dtomatic will check if a converter exists for its type and use it.
    'custom_converters' => [
        // Example:
        // 'string' => \App\Converters\JsonToArrayConverter::class,
    ],
];
```

---

🏷️ Available Attributes
-----------------------

[](#️-available-attributes)

Dtomatic supports the following PHP attributes to customize DTO mapping behavior:

### 1. `#[ArrayOf(Type::class)]`

[](#1-arrayoftypeclass)

- Use this attribute to specify the type of objects inside an array or collection property.
- Enables automatic mapping of nested collections of DTOs.

```
    use ShihabJamil\Dtomatic\Attributes\ArrayOf;

    class UserDTO
    {
        #[ArrayOf(PostDTO::class)]
        public array $posts;
    }
```

### 2. `#[Ignore]`

[](#2-ignore)

- Marks a DTO property to be ignored during mapping.
- Useful for excluding properties you do not want to populate.

```
    use ShihabJamil\Dtomatic\Attributes\Ignore;

    class PostDTO
    {
        #[Ignore]
        public string $internalNotes;
    }
```

### 3. `#[Converter(ConverterClass::class)]`

[](#3-converterconverterclassclass)

- Specifies a custom converter class for a particular property.
- The converter class must implement a `convert($value)` method returning the converted value.

```
    use ShihabJamil\Dtomatic\Attributes\Converter;

    class PostDTO
    {
        #[Converter(MyCustomConverter::class)]
        public string $specialField;
    }
```

---

🚀 Basic Usage
-------------

[](#-basic-usage)

### Example DTO class:

[](#example-dto-class)

```
namespace App\DTO;

class UserDTO
{
    public int $id;
    public string $name;
    public string $email;
}
```

### Example mapping in your controller or service:

[](#example-mapping-in-your-controller-or-service)

```
use App\Models\User;
use Dtomatic\Facades\ModelMapper;
use App\DTO\UserDTO;

$user = User::find(1);
$dto = ModelMapper::map($user, UserDTO::class);

return response()->json($dto);
```

---

📑 Nested Mapping Example
------------------------

[](#-nested-mapping-example)

```
class PostDTO {
    public int $id;
    public string $title;
    public UserDTO $author;
}

$post = Post::with('author')->find(1);
$dto = ModelMapper::map($post, PostDTO::class);
```

---

Nested Collections Mapping with #\[ArrayOf\] Attribute
------------------------------------------------------

[](#nested-collections-mapping-with-arrayof-attribute)

```
