定制 webgriffe/amp-csv 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

webgriffe/amp-csv

最新稳定版本:v0.3.0

Composer 安装命令:

composer require webgriffe/amp-csv

包简介

CSV library to use with Amp PHP framework.

README 文档

README

Build Status

CSV library to use with Amp PHP framework. Currently it implements only an iterator which allows to parse CSV rows one at a time.

Installation

Require this package using Composer:

composer require webgriffe/amp-csv

Iterator Usage

This library implements an Amp's Iterator which allows to iterate over CSV rows one at a time. Potentially it can parse very large CSV files because only small chunks are kept in memory. See the following example, given this CSV file (path/to/file.csv):

Name,Description,Price,Stock
RaspberryPi,"Raspberry PI Modell B, 512 MB",37.05,12
SanDisk Ultra SDHC,SanDisk Ultra SDHC 8 GB 30 MB/s Classe 10,6.92,54

We can have:

<?php

use Webgriffe\AmpCsv\Iterator;
use Webgriffe\AmpCsv\Parser;
use Amp\File;

require_once 'vendor/autoload.php';

\Amp\Loop::run(function () {
    $iterator = new Iterator(new Parser(yield File\open('path/to/file.csv', 'rb')));
    while (yield $iterator->advance()) {
        $rows[] = $iterator->getCurrent();
    }
    var_dump($rows);
});

And the output will be:

array(
    array(
        'Name' => 'RaspberryPi',
        'Description' => 'Raspberry PI Modell B, 512 MB',
        'Price' => 37.05,
        'Stock' => 12,
    ),
    array(
        'Name' => 'SanDisk Ultra SDHC',
        'Description' => 'SanDisk Ultra SDHC 8 GB 30 MB/s Classe 10',
        'Price' => 6.92,
        'Stock' => 54,
    ),
),

By default the iterator treats the first line as header and will use the column names to index row values. If a row has a different column number than header an exception will be thrown. If your CSV doesn't have an header as first line you can disable the header parsing by passing false as constructor's second argument:

$iterator = new Iterator(new Parser(yield File\open('path/to/file.csv', 'rb')), false);

Contributing

To contribute simply fork this repository, do your changes and then propose a pull requests. You should run coding standards check and tests as well:

vendor/bin/phpcs --standard=PSR2 src
vendor/bin/phpunit

License

This library is under the MIT license. See the complete license in the LICENSE file.

Credits

Developed by Webgriffe®. Thanks also to Niklas Keller for his help about converting ReactPHP stream events to an Amp's Iterator (see reactphp/promise-stream#14).

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固