定制 fusonic/csv-reader 二次开发

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

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

fusonic/csv-reader

Composer 安装命令:

composer require fusonic/csv-reader

包简介

Map data from CSV files to typed PHP models.

README 文档

README

License Latest Version Total Downloads php 8.2+

CSV Reader is a powerful library for PHP 8 that allows you to map data from CSV files to a strongly typed data model. It does this by utilizing PHP 8's typed properties / arguments and automatically converts string values to the types defined for your class fields.

Install

Use composer to install the lib from Packagist.

composer require fusonic/csv-reader

Requirements:

  • PHP 8.2+

Usage

Just define a class that represents your CSV's data structure and use the IndexMapping or TitleMapping attributes to define the mapping. You don't have to map all the columns, just the ones you need on your model.

class Foo
{
    #[TitleMapping('Price')]
    public float $price;

    #[TitleMapping('Price')]
    public function setPrice(float $value): void
    {
        // Not implemented
    }
}

Now use the CsvReader class to convert CSV data to your object model:

$reader = new CsvReader('myfile.csv');
foreach ($reader->readObjects(Foo::class) as $item) {
    var_dump($item);
}

Hint: You can also pass a resource instead of a file's path to CsvReader. This way you can also access files from remote filesystems, for example if you are using league/flysystem.

Supported types

  • int
  • float
  • string
  • bool (uses filter_var() with FILTER_VALIDATE_BOOLEAN and supports values like on, true, 1)
  • DateTime
  • DateTimeInterface
  • DateTimeImmutable

If you choose to use a **nullable type like ?string``** it will be mapped as null` if the CSV value is an empty string or 'null' (case insensitive).

Options

Refer to the CsvReaderOptions class to learn about options.

IntlValueConverter

If you need to unserialize floating point numbers from a specific culture, you can use the IntlValueConverter like this:

$options = new CsvReaderOptions();
$options->valueConverter = new IntlValueConverter('de-AT');

$reader = new CsvReader($options);

Note: parsing German date format is done implicitely by the default ValueConverter implementation since PHP supports passing German date formats to DateTimeInterface derivates.

Contributing

This is a subtree split of fusonic/php-extensions repository. Please create your pull requests there.

fusonic/csv-reader 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 9
  • Watchers: 9
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-09