byjg/anydataset-text
Composer 安装命令:
composer require byjg/anydataset-text
包简介
Text file abstraction dataset. Anydataset is an agnostic data source abstraction layer in PHP.
README 文档
README
| sidebar_key | anydataset-text | |||
|---|---|---|---|---|
| tags |
|
Text File Abstraction Dataset
Text file abstraction dataset. Anydataset is an agnostic data source abstraction layer in PHP.
See more about Anydataset here.
Features
- Read and parse delimited text files (CSV, etc.)
- Read and parse fixed-width text files
- Support for remote files via HTTP/HTTPS
- Conditional field parsing based on field values
- Output formatting to CSV or fixed-width formats
Documentation
- TextFileDataset - Work with delimited text files (CSV, etc.)
- FixedTextFileDataset - Work with fixed-width text files
- FixedTextDefinition - Define the structure of fixed-width text files
- Formatters - Output dataset content in specific formats
Quick Examples
Text File Delimited (CSV)
This type of files uses a delimiter to define each field. The most common format is CSV but you can use your own based on a regular expression. The class TextFileDataset has three constants with pre-defined formats:
- TextFileDataset::CSVFILE - A generic file definition. It accepts
|,,and;as delimiter. - TextFileDataset::CSVFILE_COMMA - The CSV file. It accepts only
,as delimiter. - TextFileDataset::CSVFILE_SEMICOLON - A CSV variation. It accepts only
;as delimiter.
example1.csv
Joao;Magalhaes
John;Doe
Jane;Smith
example1.php
<?php $file = "example1.csv"; $dataset = \ByJG\AnyDataset\Text\TextFileDataset::getInstance($file) ->withFields(["name", "surname"]) ->withFieldParser(\ByJG\AnyDataset\Text\TextFileDataset::CSVFILE); $iterator = $dataset->getIterator(); foreach ($iterator as $row) { echo $row->get('name'); // Print "Joao", "John", "Jane" echo $row->get('surname'); // Print "Magalhaes", "Doe", "Smith" }
Text File Delimited (CSV) - Get field names from first line
example2.csv
firstname;lastname
John;Doe
Jane;Smith
example2.php
<?php $file = "example2.csv"; // If omit `withFields` will get the field names from first line of the file $dataset = \ByJG\AnyDataset\Text\TextFileDataset::getInstance($file) ->withFieldParser(\ByJG\AnyDataset\Text\TextFileDataset::CSVFILE); $iterator = $dataset->getIterator(); foreach ($iterator as $row) { echo $row->get('firstname'); // Print "John", "Jane" echo $row->get('lastname'); // Print "Doe", "Smith" }
Text File Fixed sized columns
This file has the field defined by its position on the line. It is necessary to define the name, type, position and field length for each field to parse the file. This definition also allows setting up required values and sub-types based on a value.
The field definition is created using the FixedTextDefinition class and it has the following parameters:
$definition = new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition( $fieldName, # The field name $startPos, # The start position of this field in the row $length, # The number of characters of the field content $type, # (optional) The type of the field content. TextTypeEnum::STRING (default) or TextTypeEnum::NUMBER $requiredValue, # (optional) an array of valid values. E.g. ['Y', 'N'] $subTypes # (optional) An associative array of FixedTextDefinition. If the value matches with the key of the associative array, # then a sub set of FixedTextDefinition is processed. e.g. # [ # "Y" => [ # new FixedTextDefinition(...), # new FixedTextDefinition(...), # ], # "N" => new FixedTextDefinition(...) # ] );
Example:
<?php $file = "". "001JOAO S1520\n". "002GILBERTS1621\n"; $fieldDefinition = [ new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('id', 0, 3, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::NUMBER), new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('name', 3, 7, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::STRING), new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('enable', 10, 1, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::STRING, ['S', 'N']), // Required values --> S or N new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('code', 11, 4, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::NUMBER), ]; $dataset = new \ByJG\AnyDataset\Text\FixedTextFileDataset($file) ->withFieldDefinition($fieldDefinition); $iterator = $dataset->getIterator(); foreach ($iterator as $row) { echo $row->get('id'); echo $row->get('name'); echo $row->get('enable'); echo $row->get('code'); }
Text File Fixed sized columns with conditional type of fields
<?php $file = "". "001JOAO S1520\n". "002GILBERTS1621\n"; $fieldDefinition = [ new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('id', 0, 3, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::NUMBER), new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('name', 3, 7, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::STRING), new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition( 'enable', 10, 1, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::STRING, null, [ "S" => [ new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('first', 11, 1, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::STRING), new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('second', 12, 3, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::STRING), ], "N" => [ new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('reason', 11, 4, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::STRING), ] ] ), ]; $dataset = new \ByJG\AnyDataset\Text\FixedTextFileDataset($file) ->withFieldDefinition($fieldDefinition); $iterator = $dataset->getIterator(); foreach ($iterator as $row) { echo $row->get('id'); echo $row->get('name'); echo $row->get('enable'); echo $row->get('first'); // Not empty if `enable` == "S" echo $row->get('second'); // Not empty if `enable` == "S" echo $row->get('reason'); // Not empty if `enable` == "N" }
Read from remote URL
Both TextFileDataset and FixedTextFileDataset support reading files from remote HTTP or HTTPS URLs.
Formatters
This package implements two formatters:
- CSVFormatter - output the content as CSV File (field delimited)
- FixedSizeColumnFormatter - output the content with columns defined by length.
Click here for more information about formatters.
CSVFormatter
$formatter = new \ByJG\AnyDataset\Text\Formatter\CSVFormatter($anydataset->getIterator()); $formatter->setDelimiter(string); # Default: , $formatter->setQuote(string); # Default: " $formatter->setApplyQuote(CSVFormatter::APPLY_QUOTE_ALWAYS | CSVFormatter::APPLY_QUOTE_WHEN_REQUIRED | CSVFormatter::APPLY_QUOTE_ALL_STRINGS | CSVFormatter::NEVER_APPLY_QUOTE); # Default: APPLY_QUOTE_WHEN_REQUIRED $formatter->setOutputHeader(true|false); # Default: true $formatter->toText();
FixedSizeColumnFormatter
$fieldDefinition = [ ... ]; # See above about field definition $formatter = new \ByJG\AnyDataset\Text\Formatter\FixedSizeColumnFormatter($anydataset->getIterator(), $fieldDefinition); $formatter->setPadNumber(string); # Default: 0 $formatter->setPadString(string); # Default: space character $formatter->toText();
Install
composer require "byjg/anydataset-text"
Running Unit tests
vendor/bin/phpunit
Dependencies
flowchart TD
byjg/anydataset-text --> byjg/anydataset
Loading
byjg/anydataset-text 适用场景与选型建议
byjg/anydataset-text 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.9k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 byjg/anydataset-text 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 byjg/anydataset-text 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-24