定制 sebastian/csv-parser 二次开发

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

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

sebastian/csv-parser

Composer 安装命令:

composer require sebastian/csv-parser

包简介

Library for type-safe parsing of CSV files

README 文档

README

No Maintenance Intended Latest Stable Version CI Status codecov

sebastian/csv-parser

Library for type-safe parsing of CSV files.

Installation

You can add this library as a local, per-project dependency to your project using Composer:

composer require sebastian/csv-parser

If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:

composer require --dev sebastian/csv-parser

Usage

example.csv

1,2,3,1,0,2023-03-24
<?php declare(strict_types=1);
use SebastianBergmann\CsvParser\Parser;
use SebastianBergmann\CsvParser\Schema;
use SebastianBergmann\CsvParser\FieldDefinition;
use SebastianBergmann\CsvParser\Type;
use SebastianBergmann\CsvParser\ObjectMapper;

$schema = Schema::from(
    FieldDefinition::from(1, 'a', Type::integer()),
    FieldDefinition::from(2, 'b', Type::float()),
    FieldDefinition::from(3, 'c', Type::string()),
    FieldDefinition::from(4, 'd', Type::boolean()),
    FieldDefinition::from(5, 'e', Type::boolean()),
    FieldDefinition::from(6, 'f', Type::object(
        new class implements ObjectMapper
        {
            public function map(string $value): DateTimeImmutable
            {
                return new DateTimeImmutable($value);
            }
        }
    )),
);

$parser = new Parser;

foreach ($parser->parse('example.csv', $schema) as $record) {
    var_dump($record);
}

The example above shows how to use a Schema object to configure how a string from a line of comma-separated values is parsed into an array element:

  • 1 refers to the position of the string in the line of comma-separated values
  • 'a' specifies the key for the value in the associative array that is generated for each record
  • Type::integer() specifies the type that should be used to store the value in the associative array that is generated for each record

The following types are available:

  • boolean (Type::boolean(); uses (bool) type cast)
  • integer (Type::integer(); uses (int) type cast)
  • float (Type::float(); uses (float) type cast)
  • string (Type::string())
  • object (Type::object($mapper); $mapper is an object that implements the SebastianBergmann\CsvParser\ObjectMapper interface)

The Parser::parse() method requires two arguments:

  • The first argument, $filename, is the path to the CSV file that should be parsed
  • The second argument, $schema, is the Schema object we discussed above

Running the example shown above prints the output shown below:

array(3) {
  ["a"]=>
  int(1)
  ["b"]=>
  float(2)
  ["c"]=>
  string(1) "3"
  ["d"]=>
  bool(true)
  ["e"]=>
  bool(false)
  ["f"]=>
  object(DateTimeImmutable)#1 (3) {
    ["date"]=>
    string(26) "2023-03-24 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(13) "Europe/Berlin"
  }
}

The $parser->ignoreFirstLine() method can be used to configure the parser to ignore the first line of the CSV file.

The $parser->setSeparator() method can be used to configure the parser to use a separator different from the default ,.

The $parser->setEnclosure() method can be used to configure the parser to use an enclosure different from the default ".

The $parser->setEscape() method can be used to configure the parser to use an escape different from the default ".

Please note that this is a low maintenance project.

sebastian/csv-parser 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 41
  • 点击次数: 12
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 41
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-03-22