PHPackages                             mr-robertamoah/dto - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mr-robertamoah/dto

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mr-robertamoah/dto
==================

this will help you create dtos from requests and arrays quite easily

v1.0.7(3y ago)142MITPHPPHP &gt;=8.0

Since Nov 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/mr-robertamoah/dto)[ Packagist](https://packagist.org/packages/mr-robertamoah/dto)[ RSS](/packages/mr-robertamoah-dto/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (9)Used By (0)

**Data Transfer Objects (DTOs)**
================================

[](#data-transfer-objects-dtos)

About DTOs
----------

[](#about-dtos)

This is a [laravel](https://laravel.com) package that will help you easily create data transfer objects from requests and arrays by simple using four simple steps:

- Install package
- Create DTO through commandline
- Set public properties of the class
- Then create your DTOs from requests or arrays

The DTO sets the properties with values from request or array so that this property bag (data transfer object) can be passed on to your functions in parts of your controllers, services or even actions;

Installation
------------

[](#installation)

Install package using composer

```
    composer require mr-robertamoah/dto

```

Configuration
-------------

[](#configuration)

To start configuring the workings of this package, first publish the configuration file using:

```
    php artisan vendor publish --tag=dto-config

```

This command adds `dto.php` file in the config folder. The following shows the keys of the config file and what they do:

- **folderName**: This will help you use a folder name other than DTOs which is the default
- **attachDTO**: If this is set to `true` the name of every DTO you create will end with DTO
- **forcePropertiesOnDTO**: An exception is thrown whenever you try to set a value to a non-existent property. Setting this to `true` will dynamically create the property on the DTO object.
- **suppressPropertyNotFoundException**: When set to `true`, no DTOPropertyNotFoundException will be thrown, rather, the object will be returned.
- **suppressMethodNotFoundException**: When set to `true`, no DTOMeothdNotFoundException will be thrown, rather, the object will be returned.

Usage
-----

[](#usage)

Below are a break down of how to use the DTO

### Creating File

[](#creating-file)

You can always create a file manually and extend the `BaseDTO` class or use laravel's artisan commands.

```
    php artisan dto:make UserDTO
```

Note the following options on the command:

- `--folderName`: point to the app folder in which to create the DTO
- `--attachDTO`: whether or not to attach DTO to the name of the files and classes
- `--force`: force the creation of a file even if file already exists You can create multiple files as well.

```
    php artisan dto:make UserDTO ImageDTO
```

### Setting Properties

[](#setting-properties)

```
    class UserDTO extends BaseDTO
    {
        public $name;
        public $email;
    }
```

### Creating Object

[](#creating-object)

#### **From Request**

[](#from-request)

```
    public function create(Illuminate\Http\Request $request)
    {
        $dto = UserDTO::fromRequest($request);
        //or
        $dto = UserDTO::new()->fromRequest($request);
    }
```

Note that this request should be of type `Illuminate\Http\Request`.

#### **From Array**

[](#from-array)

```
    public function create(Illuminate\Http\Request $request)
    {
        $data = [
            'name' => 'Robert Amoah',
            'email' => 'mr_robertamoah@yahoo.com',
        ];

        $dto = UserDTO::fromArray($data);
        //or
        $dto = UserDTO::new()->fromArray($data);
    }
```

### Transfering Data

[](#transfering-data)

```
    public function create(Illuminate\Http\Request $request)
    {
        $dto = UserDTO::fromRequest($request);

        $user = UserService::createUser($dto);
    }
```

### Using The Data

[](#using-the-data)

```
    public function createUser(UserDTO $userDTO)
    {
        $user = User::create($userDTO->getFilledData());
    }
```

### Extending The Creation Methods

[](#extending-the-creation-methods)

You can extend either `fromArray` or `fromRequest` methods using `fromArrayExtension` and `fromRequestExtension` protected methods respectively. These methods will receive the first arguments (the array and request respectively) passed into the main creation methods and must return the same object;

```
    class UserDTO extends BaseDTO
    {
        public $name;
        public $email;
        public $date;

        protected function fromRequestExtenstion(Illuminate\Http\Request $request) : BaseDTO
        {
            return $this->date = new Carbon();
        }

        protected function fromArrayExtenstion(array $data) : BaseDTO
        {
            return $this->date = new Carbon();
        }
    }
```

Properties
----------

[](#properties)

This section shows you the protected properties available on the `BaseDTO` class which helps you get the best out of your dto objects.

#### **dtoDataKeys**

[](#dtodatakeys)

If you would want to get some properties other than the filled ones then you have to indicate the properties in this array

```
    // dto class
    class UserDTO extends BaseDTO
    {
        protected array $dtoDataKeys = [
            'name', 'email'
        ];
    }

    //in services, controller or actions
    public function createUser(UserDTO $userDTO)
    {
        $user = User::create($userDTO->getData());
    }
```

Note that the `getData` method will return an empty array if no property is added to the `dtoDataKeys`. If you pass `true` to the method like `getData(true)`, then properties that have been filled will be returned as an array.

#### **dtoFileKeys**

[](#dtofilekeys)

This property helps you set the appropriate property names for files to be retrieved from a request as well as easily get an array of all such properties by using the `getFiles` method.

```
    // dto class
    class UserDTO extends BaseDTO
    {
        protected array $dtoFileKeys = [
            'image1', 'image2'
        ];
    }

    // in service
    public function createUser(UserDTO $userDTO)
    {
        foreach($userDTO->getFiles() as $key => $file) {
            $file->save();
        }
    }
```

Also, `dtoDataKeys` and `dtoFileKeys` can both be set dynamically by calling the `setDataKeys` and `setFileKeys` methods respectively on an object. The append versions of these methods adds extra keys to the arrays (dtoDataKeys and dtoFileKeys).

```
    UserDTO::new()
        ->setDataKeys(['name', 'email'])
        ->setFileKeys('image1, image2');
```

#### **dtoExclude**

[](#dtoexclude)

Add the names of properties to this array if you do not want it to given a value during the creation of a DTO.

```
    // dto class
    class UserDTO extends BaseDTO
    {
        protected array $dtoExclude = [
            'image1',
        ];
    }
```

#### **dtoOnly**

[](#dtoonly)

Add the names of properties to this array if you only want them to be given a value during the creation of a DTO. In the example below, only name and emails will be given a value during the creation of the DTO.

```
    // dto class
    class UserDTO extends BaseDTO
    {
        public $name;
        public $email;
        public $age;

        protected array $dtoExclude = [
            'name', 'email'
        ];
    }
```

Note that the `dtoOnly` will take precendence over the `dtoExclude` once it has at least one entry.

Important Methods
-----------------

[](#important-methods)

These methods allow you to use the dto fluently while getting the most out of it.

#### **with\[PropertyName\]**

[](#withpropertyname)

This allows you to call a public method which starts with `with` and ends with the name of a property. The argument of this method should be the value you wish to assign to the property. The method will return the DTO after the value has be assigned.

```
    // in service
    public function createUser(UserDTO $userDTO)
    {
        $userDTO = $userDTO->withName('James Coffie');

        $user = User::create($userDTO->getData()):
    }
```

#### **addData**

[](#adddata)

This allows you to pass an array that contains property names as keys that point to values you would want assigned to the DTO. The keys of the array must match the names of the properties to which you want values assigned. The method will return the DTO after the value has be assigned.

```
    // in service
    public function createUser(UserDTO $userDTO)
    {
        $userDTO = $userDTO->addData([
            'name' => 'James Coffie',
            'email' => 'jamescoffie123@zigzag.org',
        ]);

        $user = User::create($userDTO->getData()):
    }
```

#### **new**

[](#new)

This is a static method that allows you to create an new object from the DTO class.

```
    // in controller
    public function create($request)
    {
        $userDTO = UserDTO::new();

        $user = UserService::create(
            $userDTO->setFileKeys(['image1', 'image2'])->fromRequest($request)
        ):
    }
```

#### **forceProperties**

[](#forceproperties)

The DTO will throw a DTOPropertyNotFound exception when you try to assign a value to a property not set in the DTO class. A property will be set dynamically when you use this method before creating the DTO.

```
    // in controller
    public function create($request)
    {
        $userDTO = UserDTO::new();

        $user = UserService::create(
            $userDTO->forceProperties()->fromRequest($request)
        ):
    }
```

Exceptions
----------

[](#exceptions)

These are the following exceptions that are thrown by this package:

- DTOPropertyNotFound
- DTOMethodNotFound
- DTOWrongArgument
- DTOFileAlreadyExists

Note that DTOFileAlreadyExists can only thrown in console whereas the others can only be thrown when using the DTO. You can suppress `DTOPropertyNotFound` and `DTOMethodNotFound` exceptions in the configuration file.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~28 days

Recently: every ~48 days

Total

8

Last Release

1429d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/12516890f4113b5124126754b8e883b3fc20d1d05a579049b385c4fb89f489c2?d=identicon)[mr\_robertamoah](/maintainers/mr_robertamoah)

---

Top Contributors

[![mr-robertamoah](https://avatars.githubusercontent.com/u/58409456?v=4)](https://github.com/mr-robertamoah "mr-robertamoah (13 commits)")

---

Tags

dtodata transfer objectsdtos

### Embed Badge

![Health badge](/badges/mr-robertamoah-dto/health.svg)

```
[![Health](https://phpackages.com/badges/mr-robertamoah-dto/health.svg)](https://phpackages.com/packages/mr-robertamoah-dto)
```

###  Alternatives

[zero-to-prod/data-model

Transforms Data into Type-Safe DTOs.

14226.2k32](/packages/zero-to-prod-data-model)[dereuromark/cakephp-dto

A CakePHP plugin for generating immutable Data Transfer Objects with full type safety

2988.9k3](/packages/dereuromark-cakephp-dto)[nutgram/hydrator

Hydrator for PHP 8.0+

12265.2k6](/packages/nutgram-hydrator)[cerbero/dto

Data Transfer Object (DTO)

17119.4k1](/packages/cerbero-dto)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
