Skip to content

blackbird-agency/dto-toolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dto-toolkit

Latest Stable Version Total Downloads License: MIT

The Magento 2 DTO Toolkit Module provides the tools for creating Data Transfer Objects (DTO) in Magento 2 while retaining Magento's key features such as plugins, preferences, etc.

This module aims to enhance the development experience by offering a structured approach to handling data transfer within Magento applications.

FeaturesInstallationUsageMore modules

Features

  • Easy DTO Creation: Simplifies the process of creating and managing DTOs.
  • Magento Integration: Maintains full compatibility with Magento's DI system, plugins, and preferences.
  • Auto Hydration: Provides a way to automatically hydrate your DTO with an array.

Installation

Requirements

  • PHP >= 7.4
composer require blackbird/dto-toolkit
php bin/magento setup:upgrade

In production mode, do not forget to recompile and redeploy the static resources.

Usage

To instantiate your DTO while maintains full compatibility with Magento 2, please use the provided DTOFactory

use Blackbird\DTOToolkit\Model\Factory\DTOFactory;

/** @var DTOFactory $dtoFactory **/
protected $dtoFactory;

public function __construct(
    DTOFactory $dtoFactory
) {
  $this->dtoFactory = $dtoFactory;
}

[...]

$myDtoInstance = $this->dtoFactory->create(MyDTO::class);

To automatically hydrate your DTO instance with an array

use Blackbird\DTOToolkit\Model\Factory\DTOFactory;

/** @var DTOFactory $dtoFactory **/
protected $dtoFactory;

public function __construct(
    DTOFactory $dtoFactory
) {
  $this->dtoFactory = $dtoFactory;
}

[...]

myArray = [
  'key_one' => 1,
  'key_two' => 2
];

$myDtoInstance = $this->dtoFactory->create(MyDTO::class, $myArray);

Warning, if your DTO class doesn't have the properties keyOne or keyTwo, they will not be hydrated.

About

Provide a DTO Toolkit for Magento 2

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages