PHPackages                             wguimaraes/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. wguimaraes/dto

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

wguimaraes/dto
==============

This is a simple Laravel package for create DTO classes to help manage data normalization.

0.0.1(1y ago)01MITPHPPHP ^8.2

Since Jan 4Pushed 1y ago1 watchersCompare

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

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

A Simple Package to Help Create DTOs (Data Transfer Objects)
============================================================

[](#a-simple-package-to-help-create-dtos-data-transfer-objects)

Getting started
---------------

[](#getting-started)

To install this pachage just add the dependency in your composer.json file or execute the command:

```
composer require wguimaraes/dto
```

This package aims to simplify data normalization by using the DTO pattern in a straightforward way.

Features
--------

[](#features)

Its functionalities include simple methods:

```
namespace App\Service;

use App\DTO\PersonDTO;

class ExampleService
{
    protected PersonDTO $personFromArray;
    protected PersonDTO $personFromObject;
    protected array $dtoList;
    protected array $list;

    public function __construct()
    {
        // Create a DTO object using an array that contains the properties as keys of the array
        $this->personFromArray = PersonDTO::fromArray(['first_name' => 'William', 'last_name' => 'Guimarães']);

        // Create a DTO object from another object, such as a Model class instance or a generic object that has the same properties as your DTO
        $this->personFromObject = PersonDTO::fromObject($this->personFromArray);

        // Convert a DTO object to an array of properties where the keys match the properties of the original DTO object
        $personDataArray = $this->personFromObject->toArray();

        // Create an array that contains a list of DTO objects from a previous array list
        $this->dtoList = PersonDTO::fromList([
            [
                'first_name' => 'Person 1 name',
                'last_name' => 'Person 1 last name',
                'address' => [
                    'street' => 'Street of Person 1',
                    'zip_code' => 'Zip code of Person 1',
                    'city' => 'City of Person 1'
                ]
            ],
            [
                'first_name' => 'Person 2 name',
                'last_name' => 'Person 2 last name',
                'address' => [
                    'street' => 'Street of Person 2',
                    'zip_code' => 'Zip code of Person 2',
                    'city' => 'City of Person 2'
                ]
            ]
        ]);

        // Convert the DTO list back into a common array list
        $this->list = PersonDTO::toList($this->dtoList);
    }
}
```

The PersonDTO class:

```
namespace App\DTO;

use Wguimaraes\DTO\BaseDTO;

class PersonDTO extends BaseDTO
{
    public function __construct(
        public ?string $first_name,
        public ?string $last_name,
        public ?AddressDTO $address
    )
    {}
}
```

You can nest DTO's as a properties:

```
namespace App\Service;

use App\DTO\PersonDTO;

class ExampleService
{
    public static function setPersonAddress(array $address): PersonDTO
    {
        $this->personFromArray->address = AddressDTO::fromArray($address);
        return $this->personFromArray;
    }
}
```

To create DTO classes you cancreate a DTO folder to store all your DTO classes and symply extend the BaseDTO class:

```
.
|__Dto
   |__MyDtoClass.php
   |__DataSubFolder
      |__MyOtherDtoClass.php
```

Class:

```
