Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mordilion/pipeline

Pipeline is a library to transfer data quick and reliable or to create exports with any kind of interfaces like \PDO, \SplFileObject(CSV, etc.), Arrays and your own implementation.
https://github.com/mordilion/pipeline

array configurable csv database export files import reader transfer writer

Last synced: 3 months ago
JSON representation

Pipeline is a library to transfer data quick and reliable or to create exports with any kind of interfaces like \PDO, \SplFileObject(CSV, etc.), Arrays and your own implementation.

Awesome Lists containing this project

README

        

[![Packagist](https://img.shields.io/packagist/dt/mordilion/pipeline.svg)](https://packagist.org/packages/mordilion/pipeline)

# Pipeline

## Description

Pipeline is a library to transfer data quick and reliable or to create exports with any kind of interfaces like \PDO, \SplFileObject(CSV, etc.), Arrays and your own implementation. It uses https://github.com/mordilion/Configurable to be full configurable.

## Basic Example

```php
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();
```

## Database to CSV

An example of how to export data from a database into a CSV file with additional columns.

```php
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);
});
```

## Directory listing to CSV

An example of how to export data from a database into a CSV file with additional columns.

```php
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();
```