PHP code example of pollora / datamorph

1. Go to this page and download the library: Download pollora/datamorph library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pollora / datamorph example snippets


return [
    'pipelines' => [
        'stock' => [
            'hooks' => [
                'before_extract' => [
                    App\ETL\Stock\Hooks\BeforeStockExtract::class,
                ],
                'after_extract' => [
                    // Hooks to execute after extraction
                ],
                'before_transform' => [
                    // Hooks to execute before transformation
                ],
                'after_transform' => [
                    // Hooks to execute after transformation
                ],
                'before_load' => [
                    // Hooks to execute before loading
                ],
                'after_load' => [
                    // Hooks to execute after loading
                ],
                'before_run' => [
                    App\ETL\Stock\Hooks\BeforeStockRun::class,
                ],
                'after_run' => [
                    // Hooks to execute after complete execution
                ],
            ],
        ],
        // Other pipelines...
    ],
];



declare(strict_types=1);

namespace App\ETL\Product;

use Flow\ETL\FlowContext;
use Pollora\Datamorph\Contracts\Extractor;

class ProductExtractor extends Extractor
{
    public function handle(FlowContext $context): array
    {
        // Data extraction logic
        // Returns an array of raw data
        return [];
    }
}



declare(strict_types=1);

namespace App\ETL\Product;

use Flow\ETL\FlowContext;
use Pollora\Datamorph\Contracts\Transformer;

class ProductTransformer extends Transformer
{
    public function handle(array $rows, FlowContext $context): array
    {
        // Data transformation logic
        // Receives raw data and returns transformed data
        return $rows;
    }
}



declare(strict_types=1);

namespace App\ETL\Product;

use Flow\ETL\Rows;
use Flow\ETL\FlowContext;
use Pollora\Datamorph\Contracts\Loader;
use Flow\ETL\Loader as FlowLoader;

class ProductLoader extends Loader
{
    public function handle(FlowContext $context): FlowLoader
    {
        // Data loading logic
        // Returns a Flow ETL loader
        return to_memory();
    }
}

use Pollora\Datamorph\Pipeline;
use App\ETL\Stock\StockExtractor;
use App\ETL\Stock\StockTransformer;
use App\ETL\Stock\StockLoader;

$pipeline = new Pipeline(
    'stock',
    new StockExtractor(),
    new StockTransformer(),
    new StockLoader()
);

$pipeline->run();

// config/datamorph.php
return [
    'pipelines' => [
        'stock' => [
            'hooks' => [
                'before_extract' => [
                    App\ETL\Stock\Hooks\BeforeStockExtract::class,
                ],
                'after_extract' => [
                    App\ETL\Stock\Hooks\AfterStockExtract::class,
                ],
                'before_transform' => [
                    App\ETL\Stock\Hooks\BeforeStockTransform::class,
                ],
                'after_transform' => [
                    App\ETL\Stock\Hooks\AfterStockTransform::class,
                ],
                'before_load' => [
                    App\ETL\Stock\Hooks\BeforeStockLoad::class,
                ],
                'after_load' => [
                    App\ETL\Stock\Hooks\AfterStockLoad::class,
                ],
                'before_run' => [
                    App\ETL\Stock\Hooks\BeforeStockRun::class,
                ],
                'after_run' => [
                    App\ETL\Stock\Hooks\AfterStockRun::class,
                ],
            ],
        ],
    ],
];



namespace App\ETL\Stock\Hooks;

use Closure;
use Flow\ETL\DataFrame;
use Flow\ETL\Filesystem\SaveMode;
use Pollora\Datamorph\Contracts\HookInterface;

class BeforeStockRun implements HookInterface
{
    /**
     * Execute the hook with the given dataframe.
     *
     * @param mixed $dataframe The dataframe to process
     * @param Closure|null $next The next hook to execute
     * @return mixed
     */
    public function handle(mixed $dataframe, ?Closure $next = null): mixed
    {
        // Apply hook logic
        if ($dataframe instanceof DataFrame) {
            $dataframe = $dataframe->mode(SaveMode::Overwrite);
        }
        
        // Pass to the next hook in the chain
        return $next ? $next($dataframe) : $dataframe;
    }
}

// In an ETL component (Extractor, Transformer, Loader)
public function handle(FlowContext $context): array
{   
    // Add a hook after the current operation
    $context->pipeline->after(function ($dataframe) {
        // Hook logic
        log::info("After extraction");
        return $dataframe;
    }); // The operation is automatically detected
    
    // ...
}



namespace App\ETL\Stock;

use Flow\ETL\FlowContext;
use Illuminate\Support\Facades\Log;
use Pollora\Datamorph\Contracts\Extractor;

class StockExtractor extends Extractor
{
    /**
     * Extract stock data.
     */
    public function handle(FlowContext $context): array
    {
        // Extraction logic
        return $results;
    }

    /**
     * Method executed before extraction.
     */
    public function before(mixed $dataframe, FlowContext $context): mixed
    {
        Log::info("Preparing extraction");
        return $dataframe;
    }

    /**
     * Method executed after extraction.
     */
    public function after(mixed $dataframe, FlowContext $context): mixed
    {
        Log::info("Extraction completed");
        return $dataframe;
    }
}

// config/datamorph.php
'hooks' => [
    'before_extract' => [
        App\ETL\Stock\Hooks\ValidateSourceHook::class,
    ],
]

// App\ETL\Stock\Hooks\ValidateSourceHook.php
public function handle(mixed $dataframe, ?Closure $next = null): mixed
{
    // Check if the data source is available
    if (!$this->isSourceAvailable()) {
        throw new \RuntimeException("Data source is not available");
    }
    
    return $next($dataframe);
}

// In StockExtractor
public function handle(FlowContext $context): array
{
    // Add logging hooks if in debug mode
    if (config('app.debug')) {
        $context->pipeline->before(function ($dataframe) {
            Log::debug("Starting transformation");
            return $dataframe;
        }, 'transform');
        
        $context->pipeline->after(function ($dataframe) {
            Log::debug("Transformation completed");
            return $dataframe;
        }, 'transform');
    }
    
    // ...
}

// In DatabaseExtractor
public function before(mixed $dataframe, FlowContext $context): mixed
{
    // Open database connection
    $this->connection = DB::connection('source');
    Log::info("Database connection established");
    
    return $dataframe;
}

public function after(mixed $dataframe, FlowContext $context): mixed
{
    // Close connection after extraction
    if ($this->connection) {
        $this->connection = null;
        Log::info("Database connection closed");
    }
    
    return $dataframe;
}

// config/datamorph.php - Global hooks
'hooks' => [
    'before_run' => [
        App\ETL\Global\Hooks\LogStartHook::class,
    ],
    'after_run' => [
        App\ETL\Global\Hooks\LogEndHook::class,
    ],
]

// StockExtractor.php - Before/After methods
public function before(mixed $dataframe, FlowContext $context): mixed
{
    // Extraction-specific preparation
    return $dataframe;
}

// In a component's handle method - Dynamic hooks
public function handle(FlowContext $context): array
{
    // Dynamic hook for a specific case
    if ($someCondition) {
        $context->pipeline->after(function ($dataframe) {
            // Conditional logic
            return $dataframe;
        });
    }
    
    // ...
}
bash
php artisan vendor:publish --tag=datamorph-config
bash
php artisan datamorph:make product
bash
php artisan datamorph:run stock