fbk/csvmapper

CSV parser with callbacks on fields

0.1.1 2014-12-15 17:15 UTC

This package is not auto-updated.

Last update: 2024-04-08 11:31:20 UTC


README

Official repo stats

Build Status Coverage Status Scrutinizer Quality Score

CSVMapper is a PHP library which parses CSV files.

It allows to map the file to an array by defining some file configurations and each (wanted) columns properties.

File Settings

CSVMapper needs to now where the file is located and which is the columns separator.

It il also possible to specify how many columns are expected so that it aborts if the number of columns doesn't match.

    $setting = new SettingManager();
    $config->set_setting('folder','./tests');
    $config->set_setting('filename','myfile.csv');
    $config->set_setting('separator',';');
    $config->set_setting('columns_allowed',3);

Mappings

CSVMapper extracts only the columns which are defined with mappings. With a mapping it is possible to specify the position of the column, which function to apply to the value and how to test the value.

    $mapping = new MappingManager();
    // retrieve column number 1 (counting from 0) and label it as year
    $mapping->set_mapping("year", array('key'=>1,'fn'=>FALSE,'test'=>FALSE));
    // retrieve column number 2 (counting from 0) and label it as temperature, apply to each value the function 'return floatval($input);'
    $mapping->set_mapping("temperature", array('key'=>2, 'fn'=>create_function('$input','return floatval($input);'),'test'=>FALSE));