blackbird/dto-toolkit

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.

Installs: 51

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

Type:magento2-module

1.0.0 2024-05-20 16:25 UTC

This package is auto-updated.

Last update: 2024-11-02 08:52:25 UTC


README

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.