2017 © Pedro Peláez
 

library csv-mapper

Extends league/csv with mappers and exceptions

image

rdx/csv-mapper

Extends league/csv with mappers and exceptions

  • Tuesday, June 26, 2018
  • by rudiedirkx
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

CSV mapper

Uses league\csv for reading CSV. Adds record & column mapping., (*1)

The first line is always used as header. You don't have to call setHeaderOffset(0)., (*2)

In all examples, $reader is iterated, not queried with a Statement:, (*3)

foreach ( $reader as $record ) {
    // $record is done, no more processing needed
    print_r($record);
}

Record mappers

To keep only certain columns, and discard the rest, use the KeepColumnsMapper:, (*4)

$reader = \rdx\csvmapper\Reader::createFromPath($file);
$reader->addMapper(new KeepColumnsMapper(['firstname', 'lastname', 'email']));

Create your own record mapper by implementing RecordMapper:, (*5)

class AddTimestampMapper implements RecordMapper {
    protected $time;
    public function __construct( $time ) {
        $this->time = $time;
    }
    public function map( array $record, $index ) {
        $record['timestamp'] = $this->time;
        return $record;
    }
}

$reader->addMapper(new AddTimestampMapper(time()));

Require columns

Require columns before processing iterating through the rows:, (*6)

$reader = \rdx\csvmapper\Reader::createFromPath($file);
try {
    $reader->requireColumns(['email']);
}
catch ( MissingColumnsException $ex ) {
    echo implode(', ', $ex->getColumns());
}

Column mappers

If several columns in the same row have the same mapping, use the ColumnsMapper record mapper with the ColumnMapper interface., (*7)

To format several date fields with your own date formatter:, (*8)

class DateFormatMapper implements ColumnMapper {
    public function map( array $record, $column, $index, $unset ) {
        $value = trim($record[$column]);
        $date = my_custom_date_maker($value);

        if ( !$date ) {
            // Invalid = NIL
            return null;

            // Invalid = skip field (remove from $record)
            return $unset;

            // Invalid = user error
            throw new LineException($index, "Invalid date: '$value'");
        }

        return $date;
    }
}

try {
    $reader = \rdx\csvmapper\Reader::createFromPath($file);
    $reader->addMapper(new ColumnsMapper(new DateFormatMapper(), ['birthdate', 'created_on', 'valid_until']));
}
catch ( LineException $ex ) {
    echo $ex->getMessage();
    print_r($ex->getRecord($reader));
}

This will run the DateFormatMapper max 3 times for every row. The mapper runs only for fields that exist in $record., (*9)

If you want to make a field from nothing, use RecordMapper to alter the record. ColumnMapper only alters/unsets columns., (*10)

The Versions

26/06 2018

dev-master

9999999-dev

Extends league/csv with mappers and exceptions

  Sources   Download

MIT

The Requires

 

by Rudie Dirkx

26/06 2018

1.0

1.0.0.0

Extends league/csv with mappers and exceptions

  Sources   Download

MIT

The Requires

 

by Rudie Dirkx