PHP code example of mordilion / pipeline

1. Go to this page and download the library: Download mordilion/pipeline 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/ */

    

mordilion / pipeline example snippets




use Mordilion\Pipeline\Pipeline;
use Mordilion\Pipeline\Reader\ArrayReader;
use Mordilion\Pipeline\Writer\SplFileObjectWriter;

$data = [
    ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Jane Doe', 'email' => '[email protected]'],
    ['id' => 3, 'name' => 'Max Mustermann', 'email' => '[email protected]']
];

$reader = new ArrayReader();
$reader->setData($data);

$filename = __DIR__ . '/export.csv';
$writer = new SplFileObjectWriter();
$writer->setFile(new \SplFileObject($filename, 'w'))
    ->setMode(SplFileObjectWriter::MODE_CSV);

$pipeline = new Pipeline();
$pipeline->setReader($reader)
    ->setWriter($writer)
    ->transfer();



use Mordilion\Pipeline\Pipeline;
use Mordilion\Pipeline\Reader\PDOReader;
use Mordilion\Pipeline\Writer\SplFileObjectWriter;

$reader = new PDOReader();
$reader->setPdo(new \PDO('sqlite:' . __DIR__ . '/sqlite.db'))
    ->setSql('SELECT * FROM demo');

$filename = __DIR__ . '/export.csv';
$writer = new SplFileObjectWriter();
$writer->setFile(new \SplFileObject($filename, 'w'))
    ->setMode(SplFileObjectWriter::MODE_CSV);

$pipeline = new Pipeline();
$pipeline->setReader($reader)
    ->setWriter($writer)
    ->transfer(function (array $row, ReaderAbstract $reader, WriterAbstract $writer) {
        $timestamps = [
            'created_at' => isset($row['created_at']) ? $row['created_at'] : date('Y-m-d H:i:s'),
            'updated_at' => isset($row['updated_at']) ? $row['updated_at'] : date('Y-m-d H:i:s'),
            'exported_at' => date('Y-m-d H:i:s')
        ];

        return array_merge($row, $timestamps);
    });



use Mordilion\Pipeline\Pipeline;
use Mordilion\Pipeline\Reader\ExecReader;
use Mordilion\Pipeline\Writer\SplFileObjectWriter;

$reader = new ExecReader();
$reader->setCommand('ls /');

$filename = __DIR__ . '/directory-listing.csv';
$writer = new SplFileObjectWriter();
$writer->setFile(new \SplFileObject($filename, 'w'))
    ->setMode(SplFileObjectWriter::MODE_CSV);

$pipeline = new Pipeline();
$pipeline->setReader($reader)
    ->setWriter($writer)
    ->transfer();