2017 © Pedro Peláez
 

library exporter

Library to export data in various input formats to various output formats.

image

ronrademaker/exporter

Library to export data in various input formats to various output formats.

  • Monday, January 11, 2016
  • by RonRademaker
  • Repository
  • 0 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Exporter

Library to export data in various input formats to various output formats., (*1)

Coverage Status Build Status SensioLabsInsight, (*2)

Installation

``` bash composer require ronrademaker/exporter, (*3)


# Usage ## Output an array to CSV in a file ``` php $someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $data = new ArrayData($someArray); $fileOption = new FileOption($outputFile); $exporter = new CSVExporter(); $exporter->export($data, [$fileOption]);

or, (*4)

``` php $someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ];, (*5)

$data = new ArrayData($someArray); $fileOption = new FileOption($outputFile); $exporter = new CSVExporter(); $exporter->setOption($fileOption); $exporter->setInput($data); $exporter->export();, (*6)


## Add headers to control output CSV Only ```baz``` and ```foo``` will be in the resulting CSV and the order of the columns will be as set in the HeadersOption. ``` php $someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $outputFile = 'output.csv'; $headers = new HeadersOption(['baz', 'foo']); $data = new ArrayData($someArray); $fileOption = new FileOption($outputFile); $exporter = new CSVExporter(); $exporter->export($data, [$headersOption, $fileOption]);

Output to multiple files

``` php $someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $outputFile = 'output.csv'; $otherFile = sprintf('/data/dropbox/output-%s.csv', date('Ymd'));, (*7)

$headers = new HeadersOption(['baz', 'foo']); $data = new ArrayData($someArray); $options = [ $headers, new FileOption($outputFile), new FileOption($otherFile), ];, (*8)

$exporter = new CSVExporter(); $exporter->export($data, $options);, (*9)


## Error Handling ``` php $someArray = [ ['foo' => 'foo', 'bar' => 'bar', ['impossible' => 'input']], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $outputFile = 'output.csv'; $otherFile = sprintf('/data/dropbox/output-%s.csv', date('Ymd')); $headers = new HeadersOption(['baz', 'foo']); $data = new ArrayData($someArray); $options = [ $headers, new FileOption($outputFile), new FileOption($otherFile), ]; try { $exporter = new CSVExporter(); $exporter->export($data, $options); } catch (UnsupportedDataException $e) { echo sprintf('The given input is not valid: %s', $e->getMessage()); }

Exporting multiple, similar input

``` php $someArray = [ ['foo' => 'foo', 'bar' => 'bar'], ['foo' => 'foobar', 'bar' => 'baz'], ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], ['foo' => 'bar', 'baz' => 'bar'], ['foo' => 'baz', 'bar' => 'foobar'], ]; $somePluralArray = [ ['foo' => 'foos', 'bar' => 'bars'], ['foo' => 'foobars', 'bar' => 'bazs'], ['foo' => 'foos', 'bar' => 'bars', 'baz' => 'bazs'], ['foo' => 'bars', 'baz' => 'bars'], ['foo' => 'bazs', 'bar' => 'foobars'], ];, (*10)

$headers = new HeadersOption(['baz', 'foo']); $exporter = new CSVExporter([$headers]);, (*11)

$outputFileOption = new FileOption('output.csv'); $outputPluralFileOption = new FileOption('outputs.csv');, (*12)

$inputData = new ArrayData($someArray); $inputPluralData = new ArrayData($somePlurarData);, (*13)

$exporter->setOption($outputFileOption, true); $exporter->export($inputData);, (*14)

$exporter->setOption($outputPluralFileOption, true); $exporter->export($inputPluralData); ```, (*15)

The Versions

11/01 2016

dev-master

9999999-dev

Library to export data in various input formats to various output formats.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ron Rademaker

11/01 2016

0.1.0

0.1.0.0

Library to export data in various input formats to various output formats.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ron Rademaker