luispastendev / csv-generator
Csv 文件生成器
1.1.1
2023-02-15 18:39 UTC
Requires
- php: >=7.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.14
- nunomaduro/phpinsights: ^2.7
- pestphp/pest: ^1.22
This package is not auto-updated.
Last update: 2024-09-26 02:24:06 UTC
README
Csv 格式文件生成器
安装
您可以通过 composer 命令来安装,命令如下
composer require luispastendev/csv-generator
使用
要使用这个库非常简单,只需生成文件实例并按以下方式使用
use CSVGenerator\CSVGenerator; // Especificamos la ruta de donde se generara el archivo con ext csv. // si el archivo ya existe se creara uno nuevo $file = __DIR__ . '/test.csv'; // clasico $csv_generator = new CSVGenerator; $csv_generator->create($file, ['id', 'name', 'company']); $csv_generator->add([1, 'luis', 'company1']); $csv_generator->add([2, 'foo', 'company2']); // chaining functions (new CSVGenerator)->create($file, ['id', 'name', 'company']) ->add([ [1, 'luis', 'company1'], [2, 'foo', 'company2'] ]);
添加数据
您可以在应用程序流程中批量或逐步创建数据
$file = __DIR__ . '/test.csv'; $generator = new CSVGenerator($file); // paso a paso $generator->add([1, 'luis', 'company1']); $generator->add([2, 'foo', 'company2']); // en lote $generator->add([ [1, 'luis', 'company1'], [2, 'foo', 'company2'], [3, 'bar', 'company3'] );
您也可以使用 create()
函数创建包含数据的文件
$path = __DIR__ . '/test.csv'; (new CSVGenerator)->create($path, [ [1, 'luis', 'company1'], // ... ]);
如果文件已存在,库将生成一个新的带有后缀的文件
src/ ├── test.csv ├── test_1.csv └── test_2.csv
处理现有文件
您可以处理现有文件以添加新内容
$path = __DIR__ . '/ya-existo.csv'; $generator = new CSVGenerator($path); $generator->add([1,'foo','bar']); // o tambien ... (new CSVGenerator)->setFile($path)->add([ [1,'foo','bar'], //... ]);
如果您需要,可以获取现有或生成的文件信息
$generator->setFile($path)->add([10, 'php', 'fff']); $info = $generator->getFileInfo(); // [ // 'filename' => 'ya-existo.csv', // 'basename' => 'ya-existo', // 'extension' => 'csv', // 'path' => /path/dir/ // ]
API
add
/** * Se encarga de agregar data a un archivo existente regresa * falso si ocurrio algun problema * * @param array $rows * * @return bool */ // rows puede ser un array unidimensional o bidi ej: [..] o [[...], [...]] $obj->add(array $rows);
create
/** * Se encarga de crear un archivo y agregar datos * * @param string $path * @param array $rows * @return self */ // rows puede ser un array unidimensional o bidi ej: [..] o [[...], [...]] $obj->create(string $pathfile, array $rows);
setFile
/** * Establece un archivo para trabajar sobre el. * * @param string $path * @return self */ $obj->setFile(string $path);
getFileInfo
/** * Regresa información del archivo nuevo o existente. * * @return array */ $obj->getFileInfo(string $path);
测试
这些库的测试是用 pest
编写的,要运行此项目的测试,您需要执行 composer install
并在控制台执行以下命令
./vendor/bin/pest
PASS Tests\Feature\CSVGeneratorTest
✓ it create file via constructor
✓ it create file via method create
✓ it create file when name is duplicated
✓ it create csv file with data
✓ it attempt add data to csv without file
✓ it generate correct csv format.
✓ it generate file in steps
✓ it generate file in steps with chaining functions
✓ it write to an existing file
✓ it get file name
PASS Tests\Unit\CSVGeneratorTest
✓ it add suffix to a name
✓ it generate filename with suffix
✓ it valid file fails
Tests: 13 passed