mbarquin/import-export-legacy

Helper to read csv and fixed-line files into named and validated arrays, or to export arrays into csv or fixed-line files. It uses iconv to change data codification.

v1.0.0 2016-04-06 05:58 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:24:27 UTC


README

Introduction

This is a library for importing large csv or fixed-line files, it just needs an index array and a file path, the object itself is iterable, in each loop it returns an index corresponding to the read line number and an array with data in the form of the index array, if something goes wrong it returns an exception.

Installation

You can install the component in the following ways:

Usage

    $defArray = array (
        'name'    => 20,
        'surname' => 20,
        'phone'   => 10
    );

    $oImport = new mbarquin\LegacyFile\Import('./files/contacts.csv', $defArray);

    $oImport->setIsPseudoCSV(TRUE);

    foreach ($oImport as $line => $data) {
        // This is also a valid check.
        // if(is_a($data, 'Exception') === true)

        if (is_a($data, 'mbarquin\LegacyFile\ImportException') === true) {
            // Error handling
        }

    }

First of all the class needs an index definition, it's an array with the fields name as index, and in CSV case value is an integer which represents max data length, and in fixed-line cases it's the field length to be read. In this example is called defArray.

FileImport class is configured by default to read fixed-line importation files, if we want to change this behaviour we must use this method setIsPseudoCSV(TRUE|FALSE) to set CSV import file to true.

We also need a file to read, we can use an absolute or relative path, if the file not exists or something goes wrong an exception will be raised.

The object throws errors on many cases, but data size validations or fixed-line errors will not be thrown, they are returned as ImportException objects, error handling is up to you, and interrupt or not the rest of the file import, This can be configured via setReturnValidationExceptions(TRUE|FALSE) method.

    $oImport = new mbarquin\LegacyFile\Import('./files/contacts.csv', $defArray);
    $oImport->setReturnValidationExceptions(FALSE);
    try {
        foreach ($oImport as $line => $data) {

        }
    } catch(\Exception $e) {
        // Error handling.
    }

If imported file is in another encoding, the class can transcode it via iconv. Class has three encoding literals as constants, Import::UTF8 Import::LATIN1 and Import::WINDOWS_OCCI, any encoding iconv literal will be accepted as the iconv command will be encoding the text. setTranscodification($from, $to)

    $oImport = new mbarquin\LegacyFile\Import('./files/contacts.csv', $defArray);
    $oImport->setTranscodification(
        mbarquin\LegacyFile\Import::WINDOWS_OCCI, mbarquin\LegacyFile\Import::UTF8
    );

If debug mode is on, setDebug(TRUE|FALSE), Import object will track MAX memory consuption in each iteration. This library only use the memory necessary to keep the last read line from processed file. When a new line is read, the old one is deleted from memory.

This info is stored in private properties that can be accessed with:

getMaxRealMemoryUsage()
getMaxInternalMemoryUsage()

Max memory check, which writes these properties, also can be invoked with setMaxMemoryUsage() method.

If there is an environment variable called "ENV" with value "development" or "local" mode debug will be raised on construct.

Default fgetCSV separator is ; the enclosure " and scape \ , these are the defaults, they can be changed using setCsvDefaults($csvSeparator = ";", $enclosure = '"', $escape = '\'), and verified with getCsvDefaults(), which returns an array with these indexes, separator, enclosure and escape.