mbarquin/import-export-legacy 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mbarquin/import-export-legacy

Composer 安装命令:

composer require 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.

README 文档

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.

统计信息

  • 总下载量: 23
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固