deblan/csv-validator
Composer 安装命令:
composer require deblan/csv-validator
包简介
CSV validator library
README 文档
README
CSV validator library
That uses the constraints of Symfony framework: http://symfony.com/doc/current/reference/constraints.html.
Installation
You need composer:
composer require deblan/csv-validator
Example
<?php
use Deblan\Csv\CsvParser;
use Deblan\CsvValidator\Validator;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
require __DIR__.'/vendor/autoload.php';
// Initialisation of the validator
$validator = new Validator();
// The first field must contain an email
$validator->addFieldConstraint(0, new Email());
// The second field must contain a date
$validator->addFieldConstraint(1, new Date());
// Validate the legend
$validator->setExpectedHeaders(['foo', 'bar', 'bim']);
// An line must contain 3 columns
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
if (count($data) !== 6) { // 6 because of the legend (3 fields * 2)
$context->addViolation('The line must contain 3 columns');
}
}));
// Initialisation of the parser
$parser = new CsvParser();
$parser->setHasHeaders(true);
$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv'));
if ($validator->isValid() === false) {
foreach ($validator->getErrors() as $error) {
$line = $error->getLine();
$column = $error->getColumn();
$message = $error->getViolation()->getMessage();
echo <<<EOF
<ul>
<li>Line: $line</li>
<li>Column: $column</li>
<li>
<p>$message</p>
</li>
</ul>
EOF;
}
}
Run example.php and see results:
<ul>
<li>Line: 1</li>
<li>Column: </li>
<li>
<p>Invalid legend.</p>
</li>
</ul>
<ul>
<li>Line: 4</li>
<li>Column: </li>
<li>
<p>The line must contain 3 columns</p>
</li>
</ul>
<ul>
<li>Line: 2</li>
<li>Column: 1</li>
<li>
<p>This value is not a valid email address.</p>
</li>
</ul>
<ul>
<li>Line: 3</li>
<li>Column: 2</li>
<li>
<p>This value is not a valid date.</p>
</li>
</ul>
Contributors
- Simon Vieille
- Gautier Deruette
deblan/csv-validator 适用场景与选型建议
deblan/csv-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.71k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 deblan/csv-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 deblan/csv-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2016-05-20