承接 rdx/csv-mapper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

rdx/csv-mapper

最新稳定版本:1.2

Composer 安装命令:

composer require rdx/csv-mapper

包简介

Extends league/csv with mappers and exceptions

README 文档

README

Uses league\csv for reading CSV. Adds record & column mapping.

The first line is always used as header. You don't have to call setHeaderOffset(0).

In all examples, $reader is iterated, not queried with a Statement:

foreach ( $reader as $record ) {
	// $record is done, no more processing needed
	print_r($record);
}

Record mappers

To keep only certain columns, and discard the rest, use the KeepColumnsMapper:

$reader = \rdx\csvmapper\Reader::createFromPath($file);
$reader->addMapper(new KeepColumnsMapper(['firstname', 'lastname', 'email']));

Create your own record mapper by implementing RecordMapper:

class AddTimestampMapper implements RecordMapper {
	protected $time;
	public function __construct( $time ) {
		$this->time = $time;
	}
	public function map( array $record, $index ) {
		$record['timestamp'] = $this->time;
		return $record;
	}
}

$reader->addMapper(new AddTimestampMapper(time()));

Require columns

Require columns before processing iterating through the rows:

$reader = \rdx\csvmapper\Reader::createFromPath($file);
try {
	$reader->requireColumns(['email']);
}
catch ( MissingColumnsException $ex ) {
	echo implode(', ', $ex->getColumns());
}

Column mappers

If several columns in the same row have the same mapping, use the ColumnsMapper record mapper with the ColumnMapper interface.

To format several date fields with your own date formatter:

class DateFormatMapper implements ColumnMapper {
	public function map( array $record, $column, $index, $unset ) {
		$value = trim($record[$column]);
		$date = my_custom_date_maker($value);
		
		if ( !$date ) {
			// Invalid = NIL
			return null;
			
			// Invalid = skip field (remove from $record)
			return $unset;
			
			// Invalid = user error
			throw new LineException($index, "Invalid date: '$value'");
		}
		
		return $date;
	}
}

try {
	$reader = \rdx\csvmapper\Reader::createFromPath($file);
	$reader->addMapper(new ColumnsMapper(new DateFormatMapper(), ['birthdate', 'created_on', 'valid_until']));
}
catch ( LineException $ex ) {
	echo $ex->getMessage();
	print_r($ex->getRecord($reader));
}

This will run the DateFormatMapper max 3 times for every row. The mapper runs only for fields that exist in $record.

If you want to make a field from nothing, use RecordMapper to alter the record. ColumnMapper only alters/unsets columns.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-06-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固