定制 popphp/pop-csv 二次开发

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

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

popphp/pop-csv

Composer 安装命令:

composer require popphp/pop-csv

包简介

Pop CSV Component for Pop PHP Framework

README 文档

README

Build Status Coverage Status

Join the chat at https://discord.gg/TZjgT74U7E

Overview

pop-csv provides a streamlined way to work with PHP data and the CSV format.

It is a component of the Pop PHP Framework.

Install

Install pop-csv using Composer.

composer require popphp/pop-csv

Or, require it in your composer.json file

"require": {
    "popphp/pop-csv" : "^4.2.1"
}

Top

Quickstart

Create a CSV string

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data      = new Pop\Csv\Csv($phpData);
$csvString = $data->serialize();

The $csvString variable now contains:

first_name,last_name
Bob,Smith
Jane,Smith

Create data from a CSV string

You can either pass the data object a direct string of serialized data or a file containing a string of serialized data. It will detect which one it is and parse it accordingly.

$csv     = new Pop\Csv\Csv($csvString);
$phpData = $csv->unserialize();

Top

Options

Where serializing or unserializing CSV data, there are a set of options available to tailor the process:

$options = [
    'exclude'   => ['id'],    // An array of fields to exclude
    'include'   => ['email'], // An array of fields to explicitly include, omitting all others
    'delimiter' => ',',       // Delimiter defaults to ',' - could be "\t" or something else
    'enclosure' => '"',       // Default string enclosure, i.e. "my data","other data"
    'escape'    => '"',       // String character to escape in the data, i.e. "my ""data"" here"
    'fields'    => true,      // Include the field names in the first row 
    'newline'   => true,      // Allow newlines in a data cell. Set as false to trim them
    'limit'     => 0,         // Character limit of a data cell. 0 means no limit
    'map'       => [],        // Array key of a single array value to map to the data cell value
    'columns'   => [],        // Array key of a multidimensional array value to map and join into the data cell value
];

Map/Columns Example

$users = [
    'id'       => 1,
    'username' => 'testuser',
    'country'  => [
        'name' => 'United States',
        'code' => 'US'
    ],
    'roles'    => [
        ['id' => 1, 'name' => 'Admin'],
        ['id' => 2, 'name' => 'Staff'],
    ]
];
$options = [
    'map' => [
        'country' => 'code'
    ],
    'columns' => [
        'roles' => 'name'
    ]
]

Pass the options array to constructor method:

$data      = new Pop\Csv\Csv($users, $options);
$csvString = $data->serialize();
echo $csvString;

The above will output the following CSV data:

id,username,country,roles
1,testuser,US,"Admin,Staff"

Top

Output CSV

Write to File

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data = new Pop\Csv\Csv($phpData);
$data->writeToFile('/path/to/file.csv');

Output to HTTP

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data = new Pop\Csv\Csv($phpData);
$data->outputToHttp('my-file.csv');
Force download of file

Pass a true boolean as the second parameter, which forces attachment for the Content-Disposition header.

$data->outputToHttp('my-file.csv', true);
Additional HTTP headers

Additional HTTP headers can be passed to the third parameter:

$data->outputToHttp('my-file.csv', false, ['X-Header' => 'some-header-value']);

Top

Append Data

In the case of working with large data sets, you can append CSV data to an existing file on disk. This prevents loading large amounts of data into memory that may exceed the PHP environment's limits.

us Pop\Csv\Csv;

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

Csv::appendDataToFile('my-file.csv', $data);

Top

popphp/pop-csv 适用场景与选型建议

popphp/pop-csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.21k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「csv」 「pop」 「pop php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 popphp/pop-csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 popphp/pop-csv 我们能提供哪些服务?
定制开发 / 二次开发

基于 popphp/pop-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-02-22