This PHP 5.3 library offers a way to read data from, and write data to, a range or file formats and media. During the import, you can also manipulate your data with filters and converters.
This library is available on Packagist.
To install it, add the following to your composer.json:
"require": {
...
"ddeboer/data-import": "dev-master",
...
}
And run $ php composer.phar install.
If you want to use this library in a Symfony2 project, you may choose to use the DdeboerDataImportBundle instead.
- Create an
splFileObjectfrom the source data. You can also use asourceobject to retrieve thissplFileObject: construct a source and addsource filters, if you like. - Construct a
readerobject and pass ansplFileObjectto it. - Construct a
workflowobject and pass the reader to it. Add at least onewriterobject to this workflow. You can also addfiltersandconvertersto the workflow. - Process the workflow: this will read the data from the reader, filter and convert the data, and write it to the writer(s).
An example:
use Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Source\Http;
use Ddeboer\DataImport\Source\Filter\Unzip;
use Ddeboer\DataImport\Reader\CsvReader;
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;
(...)
// Create the source; here we use an HTTP one
$source = new Http('http://www.opta.nl/download/registers/nummers_csv.zip');
// As the source file is zipped, we add an unzip filter
$source->addFilter(new Unzip('nummers.csv'));
// Retrieve the \SplFileObject from the source
$file = $source->getFile();
// Create and configure the reader
$csvReader = new CsvReader($file);
$csvReader->setHeaderRowNumber(0);
// Create the workflow
$workflow = new Workflow($csvReader);
$dateTimeConverter = new DateTimeValueConverter();
// Add converters to the workflow
$workflow
->addValueConverter('twn_datumbeschikking', $dateTimeConverter)
->addValueConverter('twn_datumeind', $dateTimeConverter)
->addValueConverter('datummutatie', $dateTimeConverter)
// You can also add closures as converters
->addValueConverter('twn_nummertm',
new \Ddeboer\DataImport\ValueConverter\CallbackValueConverter(
function($input) {
return str_replace('-', '', $input);
}
)
->addValueConverter('twn_nummervan',
new \Ddeboer\DataImport\ValueConverter\CallbackValueConverter(
function($input) {
return str_replace('-', '', $input);
}
)
// Use one of the writers supplied with this bundle, implement your own, or use
// a closure:
->addWriter(new \Ddeboer\DataImport\Writer\CallbackWriter(
function($csvLine) {
var_dump($csvLine);
}
);
// Process the workflow
$workflow->process();
