定制 aymericcucherousset/csv-helper 二次开发

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

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

aymericcucherousset/csv-helper

最新稳定版本:v0.0.1

Composer 安装命令:

composer require aymericcucherousset/csv-helper

包简介

Simplify CSV with php

README 文档

README

A small PHP library to read, hydrate and write CSV files with a convenient API.

Requirements

  • PHP 8.3+
  • Composer (for installing dev deps and running scripts)

Installation

Install dependencies with Composer:

composer require aymericcucherousset/csv-helper

Usage examples

The library provides three main helpers: a reader, a hydrator and a writer.

CsvReader

Read rows from a CSV file. By default the reader expects a header row.

use Aymericcucherousset\CsvHelper\Reader\CsvReader;
use Aymericcucherousset\CsvHelper\Options\CsvOptions;

$reader = CsvReader::fromPath('/path/to/file.csv', new CsvOptions(hasHeader: true));
foreach ($reader->rows() as $row) {
    // $row is an associative array when hasHeader=true
    var_dump($row);
}

CsvHydrator

Hydrate rows into objects using PHP attributes (CsvColumn) or header-to-property mapping.

use Aymericcucherousset\CsvHelper\Hydrator\CsvHydrator;

$hydrator = new CsvHydrator($reader);
foreach ($hydrator->hydrate(MyClass::class) as $obj) {
    // $obj is an instance of MyClass
}

// or load all into memory
$all = $hydrator->hydrateAll(MyClass::class);

CsvWriter

Write indexed rows or arrays of objects to CSV. Accepts iterable rows including Traversable (ArrayIterator) and generators.

use Aymericcucherousset\CsvHelper\Writer\CsvWriter;
use Aymericcucherousset\CsvHelper\Options\CsvOptions;

$writer = CsvWriter::fromPath('/tmp/out.csv', new CsvOptions(), false);
$writer->writeRows([
    ['Alice', '30'],
    ['Bob', '25'],
], ['name','age']);

// write objects by property or getters
$writer->writeObjects([$obj1, $obj2], ['name','age'], ['name','age']);

Notes on behavior

  • CsvReader::rows() yields associative arrays when hasHeader=true, else indexed arrays.
  • Empty CSV lines: when skipEmptyLines is true (default) empty rows are skipped. Tests demonstrate behavior when skipEmptyLines=false.
  • CsvHydrator supports converters via attribute configuration and basic builtin casting for scalar typed properties (int, float, bool, string).
  • CsvWriter::writeRows() supports rows that are \Traversable (they are converted with iterator_to_array).

Contributing

This README is intended for users of the library. If you'd like to contribute, please read the full contributor guidelines in CONTRIBUTING.md which explains the PR process, testing, coding standards and useful commands.

Quick checklist for contributors:

  • Run the test suite: composer test or vendor/bin/phpunit.
  • Run static analysis: composer phpstan.
  • Run style checks and fix: composer lint / composer lint-fix.
  • Add unit tests for any new behavior or bug fix.

Then open a pull request explaining the change and the added tests.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-01