xaerobiont/transfer-container

3.0.0 2022-12-11 14:54 UTC

This package is auto-updated.

Last update: 2024-05-11 18:02:08 UTC


README

PHP container for transferring DTO between services

Latest Stable Version Total Downloads

Goals

  • Allows to transfer different DTOs into single package
  • Provides mapping mechanism. I.e. when sender and receiver has different DTO namespace or even classes
  • Compress data packs
  • Very simple, lightweight and vendor-independent

Installation

{
  "require": {
    "xaerobiont/transfer-container": "^2"
  }
}

Usage

For more detailed usage examples see /tests

use Xaerobiont\TransferContainer\Transferable;
use Xaerobiont\TransferContainer\TransferContainer;

class MyDTO implements Transferable {}
class YourDTO implements Transferable {}
class ThemDTO implements Transferable {}

$package = [];
for ($i = 1; $i <= 100; $i++) {
    $package[] = new MyDTO();
    $package[] = new YourDTO();
    $package[] = new ThemDTO();
}

$container = new TransferContainer();
$container->put($package);

$transfer = $container->pack();
$container->clear();

// receiver side
$map = [
    YourDTO::class => OtherDTO::class
];
foreach (TransferContainer::unpack($transfer, $map) as $item) {
    // $item is MyDTO/OtherDTO/ThemDTO object
}