定制 zumba/csv-policy 二次开发

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

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

zumba/csv-policy

Composer 安装命令:

composer require zumba/csv-policy

包简介

Simple, policy-driven csv rules validation

README 文档

README

Build Status

CsvPolicy is a simple, policy-based validation library for CSV files. Create rules for columns in a CSV file and the validator will load them, parse the file, and report rule violations.

CsvPolicy requires PHP >= 5.4

Example

Suppose you have the following business requirements for a CSV file named products.csv that will be uploaded to your application:

  1. It must contain the following column names: id, value
  2. It must contain at least one of the following columns: upc, code, stock
  3. The id column's values must be unique.
  4. The value column must only contain numeric values

CsvPolicy allows you to model these rules as classes. Required and optional fields are handled by the Validator class directly, so we only need to define rules for the id column and the value column.

CsvPolicy uses Traits for common validation operations (e.g. checking all values in a column are unique). The folowing rule will ensure that the values in the id field are unique:

namespace Zumba\CsvPolicy\Rule\Products;

use \Zumba\CsvPolicy\Behavior;

class Id extends \Zumba\CsvPolicy\Rule {

	use Unique;

	public function getErrorMessage($input) {
		return 'Id must be unique.  Duplicate found: ' . $input;
	}
}

You can override the Rule::validationLogic method to define custom validation for that column. The validator will use this method to check all of the values in the CSV for the column.

namespace Zumba\CsvPolicy\Rule\Products;

class Value extends \Zumba\CsvPolicy\Rule {
	public function getErrorMessage($input) {
		return 'Value must only contain numeric values. Non-numeric value found: ' . $input;
	}
	public function validationLogic($input) {
		return is_numeric($input);
	}
}

Finally, you configure your validator and use it to check if the CSV file is valid:

$validator = new \Zumba\CsvPolicy\Validator();
$validator->config([
	'rulesPath' => './path/to/custom/rules'
	'requiredFields' => [

		// string field names are always required
		'id', 'value',

		// arrays of field names indicate that at least one field is required, but not all
		['upc', 'code', 'stock']
	]
]);

$valid = $validator->isValid('./path/to/products.csv');
if (!$valid){
	// errors is an array of rules violations
	$errors = $validator->getErrors();
}

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 6
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-01-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固