alexskrypnyk/csvtable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

alexskrypnyk/csvtable

最新稳定版本:1.2.0

Composer 安装命令:

composer require alexskrypnyk/csvtable

包简介

PHP class to parse and format CSV content.

README 文档

README

Yourproject logo

PHP class to parse and format CSV content

GitHub Issues GitHub Pull Requests Test codecov GitHub release (latest by date) LICENSE Renovate

Features

  • Single-file class to manipulate CSV table.
  • Formatters for CSV, text table and Markdown table.
  • Support for a custom formatter.
  • Column manipulation: reorder, filter, and exclude columns.

Installation

composer require alexskrypnyk/csvtable

Usage

Given a CSV file with the following content:

col11,col12,col13
col21,col22,col23
col31,col32,col33      

From string

$csv = file_get_contents($csv_file);
// Format using the default formatter.
print (new CsvTable($csv))->format();

will produce identical CSV content by default:

col11,col12,col13
col21,col22,col23
col31,col32,col33      

From file

print (CsvTable::fromFile($file))->format();

will produce identical CSV content by default:

col11,col12,col13
col21,col22,col23
col31,col32,col33

Using text_table formatter

print (CsvTable::fromFile($file))->format('text_table');

will produce table content:

col11|col12|col13
-----------------
col21|col22|col23
col31|col32|col33     

Using text_table formatter without a header

print (CsvTable::fromFile($file))->withoutHeader()->format('text_table');

will produce table content:

col11|col12|col13
col21|col22|col23
col31|col32|col33     

Using markdown_table formatter

print (CsvTable::fromFile($file))->format('markdown_table');

will produce Markdown table:

| col11 | col12 | col13 |
|-------|-------|-------|
| col21 | col22 | col23 |
| col31 | col32 | col33 |     

Custom formatter as an anonymous callback

print (CsvTable::fromFile($file))->format(function ($header, $rows, $options) {
  $output = '';

  if (count($header) > 0) {
    $output = implode('|', $header);
    $output .= "\n" . str_repeat('=', strlen($output)) . "\n";
  }

  return $output . implode("\n", array_map(static function ($row): string {
    return implode('|', $row);
  }, $rows));
});

will produce CSV content:

col11|col12|col13
=================
col21|col22|col23
col31|col32|col33     

Custom formatter as a class with default format method

print (CsvTable::fromFile($file))->withoutHeader()->format(CustomFormatter::class);

Custom formatter as a class with a custom method and options

$formatter_options = ['option1' => 'value1', 'option2' => 'value2'];
print (CsvTable::fromFile($file))->withoutHeader()->format([CustomFormatter::class, 'customFormat'], $formatter_options);

Column Manipulation

Given a CSV file with the following content:

Name,Age,City,Country
John,30,New York,USA
Jane,25,London,UK

Reorder columns with columnOrder()

Reorder columns by specifying the desired order. Columns not specified are appended in their original order.

print (CsvTable::fromFile($file))->columnOrder(['City', 'Name'])->format('markdown_table');

will produce:

| City     | Name | Age | Country |
|----------|------|-----|---------|
| New York | John | 30  | USA     |
| London   | Jane | 25  | UK      |

Filter to specific columns with onlyColumns()

Keep only the specified columns in the output.

print (CsvTable::fromFile($file))->onlyColumns(['Name', 'City'])->format('markdown_table');

will produce:

| Name | City     |
|------|----------|
| John | New York |
| Jane | London   |

Exclude columns with withoutColumns()

Exclude specified columns from the output.

print (CsvTable::fromFile($file))->withoutColumns(['Age', 'Country'])->format('markdown_table');

will produce:

| Name | City     |
|------|----------|
| John | New York |
| Jane | London   |

Using column indices

All column methods accept both column names (strings) and zero-based indices (integers).

// Using indices
print (CsvTable::fromFile($file))->columnOrder([2, 0])->format();

// Using mixed names and indices
print (CsvTable::fromFile($file))->onlyColumns(['Name', 2])->format();

Combining column transformations

Column transformations are applied in order: onlyColumns()withoutColumns()columnOrder().

print (CsvTable::fromFile($file))
  ->withoutColumns(['Age'])
  ->columnOrder(['Country', 'City'])
  ->format('markdown_table');

will produce:

| Country | City     | Name |
|---------|----------|------|
| USA     | New York | John |
| UK      | London   | Jane |

Reset column transformations

$table = CsvTable::fromFile($file);

// Apply and use transformations
$table->columnOrder(['City', 'Name']);
print $table->format();

// Reset and format without transformations
print $table->resetColumns()->format();

// Individual reset methods are also available:
// $table->resetColumnOrder();
// $table->resetOnlyColumns();
// $table->resetWithoutColumns();

Maintenance

composer install
composer lint
composer test

This repository was created using the Scaffold project template

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2023-05-20