承接 dsa-io/csv-validator 相关项目开发

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

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

dsa-io/csv-validator

Composer 安装命令:

composer require dsa-io/csv-validator

包简介

A CSV Validator based on Laravel's built-in Validator

README 文档

README

Execute composer command.

composer require dsa-io/csv-validator:1.*

Register the service provider in app.php

'providers' => [
    ...Others...,
    Dsaio\CsvValidator\CsvValidatorServiceProvider::class,
]

Also alias

'aliases' => [
    ...Others...,  
    'CsvValidator' => Dsaio\CsvValidator\Facades\CsvValidator::class,
]

Basic usage

$csv_path = 'test.csv';
$rules = [
    0 => 'required',
    1 => 'required|integer',
    2 => 'required|min:4'
];
$csv_validator = CsvValidator::make($csv_path, $rules);

if($csv_validator->fails()) {
    $errors = $csv_validator->getErrors();
}    

Rules

You can set keys instead of indexes like so.

$rules = [
    'First Name' => 'required',
    'Last Name' => 'required',
    'Email' => 'required|email'
];

In this case, the heading row of the CSV need to have First Name, Last Name and Email.
And This keys will be used as attribute names for error message.

  • See the details of the rules.

Trimming

You can set the 3rd argument a boolean value. If it is set to true, it will trim the cells of the csv. (Default: true)

CsvValidator::make($csv_path, $rules, true, 'SJIS-win');

Encoding

You can set a specific encoding as the 4th argument. (Default: UTF-8)

CsvValidator::make($csv_path, $rules, 'SJIS-win');

Error messages

You can get error messages after calling fails().

$errors = $csv_validator->getErrors();

foreach ($errors as $row_index => $error) {

    foreach ($error as $col_index => $messages) {

        echo 'Row '. $row_index .', Col '.$col_index .': '. implode(',', $messages) .'<br>';

    }

}

Exception

If the validator is expecting a heading row (i.e it receives an associative rules array) and the CSV is empty, an exception will be thrown.

License

This package is licensed under the MIT License.

Copyright 2017 DSA

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固