leomarquine / php-etl

Extract, Transform and Load data using PHP.
MIT License
178 stars 81 forks source link

Callback transformer #26

Closed kasperg closed 3 years ago

kasperg commented 4 years ago

Thanks for sharing PHP ETL. I enjoyed the approach to data handling and it helped me migrate between two databases provided by different systems.

The number of transformers is limited and it seems unreasonable to expect the library to provide something that covers every need.

Would you be interested in a transformer which lets the user provide a callback with an anonymous function á la:


<?php

use Marquine\Etl\Row;
use Marquine\Etl\Transformers\Transformer;

class CallbackTransformer extends Transformer
{

  /** @var callable */
  protected $callback;

  protected $availableOptions = [
    'callback'
  ];

  /**
   * Transform the given row.
   *
   * @param \Marquine\Etl\Row $row
   *
   * @return void
   */
  public function transform(Row $row)
  {
    call_user_func($this->callback, $row);
  }
}
Naelpuissant commented 4 years ago

I agree ! But I think you can create your own Transformers just by binding it to the container

use Marquine\Etl\Container;
use MyTransformer;

$container = Container::getInstance();
$container->bind('my_transformer', MyTransformer::class);
kasperg commented 4 years ago

You can. Based on my brief experience with PHP-ETL many of my transformations are <5 LOC. I find it a bit excessive to create a class for each.

sekjal commented 4 years ago

I've also found this need, and created a Callback Transformer as well (nearly identical code!). My functions were placed in a single library file for each project, as each was very bespoke to the particular data.

ecourtial commented 4 years ago

Hi @kasperg if you want, we forked the library here and we fixed some issues and added some features. The changelog is available here. Our objective is to keep this great library alive. If you find any bug or want to contribute to keep the package alive, do not hesitate!

kasperg commented 4 years ago

@ecourtial :+1: My current use case for php-etl has passed but you are welcome to lift my suggestion to your fork.