2017 © Pedro Peláez
 

library csv

Coercive Utility Csv

image

coercive/csv

Coercive Utility Csv

  • Wednesday, February 7, 2018
  • by Coercive
  • Repository
  • 1 Watchers
  • 1 Stars
  • 817 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 14 % Grown

The README.md

Coercive Csv

  • The CSV Importer allows you to easily import CSV files by parsing them to array with setted labels.
  • The CSV Exporter allows you to easily export array to CSV files.

Get

composer require coercive/csv

IMPORTER

use Coercive\Utility\Csv\Importer;

# INIT CSV OBJECT
$csv = new Importer('path/name.csv');

# AUTO DETECT DELIMITER
$csv->detectDelimiter(int test nb line : 10 , array delimiter test list [';', ',']);

# OFFSET SEEK LINE (from start or current position)
$csv->seek(5, true|false);

# PARSE HEADER
$csv->parseHeader();

# CUSTOM CELLS NAME
$csv->setHeader([
    'cell_name_1',
    'cell_name_2',
    'cell_name_3',
    'cell_name_4',
    ...
]);

# YOU CAN SPECIFY TO NOT RETRIEVE OPTIONAL CELLS BY SETTING NULL
$csv->setHeader([
    'cell_name_1',
    NULL,
    'cell_name_3',
    NULL,
    ...
]);

# GET CSV TO ARRAY
$array = $csv->get();

# CUSTOM CELL PROCESS
$csv->callback(function($value, $cellKey, $currentLine) {

    /** 
     * Receive 3 arguments :
     * function( (string) $value , (string) $cellKey , (int) $currentLine )
     *
     * The cellKey is allways a string, even if numeric column name (for strict compare)
     *
     * Your callable should return the processed value of the current cell
     */

    return "Line : $currentLine, cell : $cellKey, value : $value";
});

EXPORTER

use Coercive\Utility\Csv\Exporter;

# INIT CSV OBJECT (csv, tsv etc...)
$csv = new Exporter('/path/to/csv/file.csv');

# OPTIONS
$csv = new Exporter('/path/to/csv/file.csv', delimiter : ',', enclosure : '"', mysqlnull false);

# HEADER [optional]
$csv->addHeader(['name_col_1', 'name_col_2', 'name_col_3' ... ]);

# SAVE ONE LINE
foreach(x as y) $csv->addLine(['col_1', 'col_2', 'col_3' ... ]);

# SAVE MULTIPLE LINES
$csv->addLines([
    ['col_1', 'col_2', 'col_3' ... ],
    ['col_1', 'col_2', 'col_3' ... ],
    ['col_1', 'col_2', 'col_3' ... ],
    ['col_1', 'col_2', 'col_3' ... ]
    ...
]);

# GET ERROR
$csv->getErrors();

# CLOSE MANUALLY OR AUTO
$csv->close();
# OR
unset($csv);

The Versions

07/02 2018

dev-master

9999999-dev http://coercive.fr

Coercive Utility Csv

  Sources   Download

MIT GNU

The Requires

  • php >=7

 

07/02 2018

0.0.3

0.0.3.0 http://coercive.fr

Coercive Utility Csv

  Sources   Download

MIT

The Requires

  • php >=7

 

25/05 2017

0.0.2

0.0.2.0 http://coercive.fr

Coercive Utility Csv

  Sources   Download

GNU

The Requires

  • php >=7

 

25/05 2017

0.0.1

0.0.1.0 http://coercive.fr

Coercive Utility Csv

  Sources   Download

GNU

The Requires

  • php >=7