flavioheleno/bank-utils

银行工具:CNAB处理,票解析和验证

dev-main 2022-12-27 12:31 UTC

README

简单直观的银行工具。

使用composer安装

composer require flavioheleno/bank-utils

解析可输入行

$line = '00190000090281913600966281313172600000000000000';
$boleto = BankUtils\Boleto\Parser::fromLine($line);

// string(3) "001"
$boleto->getIssuerBank();

// int(9)
$boleto->getCurrency();

// string(5) "00000"
$boleto->getIssuerReserve1();

// int(9)
$boleto->getCheckDigit1();

// string(10) "0281913600"
$boleto->getIssuerReserve2();

 // int(9)
$boleto->getCheckDigit2();

// string(10) "6628131317"
$boleto->getIssuerReserve3();

 // int(2)
$boleto->getCheckDigit3();

// int(6)
$boleto->getGeneralCheckDigit();

// object(DateTimeImmutable)#2380 (3) {
//   ["date"]=>
//   string(26) "1997-10-07 00:00:00.000000"
//   ["timezone_type"]=>
//   int(3)
//   ["timezone"]=>
//   string(17) "America/Sao_Paulo"
// }
$boleto->getDueDate();

// object(Money\Money)#2376 (2) {
//   ["amount":"Money\Money":private]=>
//   string(1) "0"
//   ["currency":"Money\Money":private]=>
//   object(Money\Currency)#2379 (1) {
//     ["code":"Money\Currency":private]=>
//     string(3) "BRL"
//   }
// }
$boleto->getAmount();

验证可输入行

$line = '00190000090281913600966281313172600000000000000';

// bool(true)
BankUtils\Boleto\Validator::checkLine($line);

您还可以验证解析后的票

$line = '00190000090281913600966281313172600000000000000';
$boleto = BankUtils\Boleto\Parser::fromLine($line);

// bool(true)
BankUtils\Boleto\Validator::checkBoleto($boleto);

CNAB文件

CNAB解析依赖于Provider实现,这仅仅是一些字段命名和大小设置。

示例请见这里

解析CNAB文件

$filePath = '/path/to/file.cnab';
$provider = BankUtils\Cnab\Provider\Febraban\Cnab240::class;

$cnabFile = BankUtils\Cnab\Reader::fromFile($filePath, $provider);

或者,您也可以从stringarray进行解析

$filePath = '/path/to/file.cnab';
$provider = BankUtils\Cnab\Provider\Febraban\Cnab240::class;

$str = file_get_contents($filePath);
$cnabFile = BankUtils\Cnab\Reader::fromString($str, $provider);

$arr = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$cnabFile = BankUtils\Cnab\Reader::fromArray($arr, $provider);

写入CNAB文件

$cnabFile = new BankUtils\Cnab\Container\File(...);
$filePath = '/path/to/file.cnab';
$provider = BankUtils\Cnab\Provider\Febraban\Cnab240::class;

$bool = BankUtils\Cnab\Writer::toFile($filePath, $cnabFile, $provider);

或者,您也可以写入到stringarray

$cnabFile = new BankUtils\Cnab\Container\File(...);
$provider = BankUtils\Cnab\Provider\Febraban\Cnab240::class;

$str = BankUtils\Cnab\Writer::toString($cnabFile, $provider);

$arr = BankUtils\Cnab\Writer::toArray($cnabFile, $provider);

辅助工具

银行代码

此辅助工具非常适合与票和CNAB一起使用,因为它们仅携带银行代码。

检查代码有效性

// bool(true)
BankUtils\Common\BankCode::validCode('001');


// bool(false)
BankUtils\Common\BankCode::validCode('000');

获取银行名称

// string(20) "Banco do Brasil S.A."
BankUtils\Common\BankCode::getName('001');

获取银行URL

// string(13) "www.bb.com.br"
BankUtils\Common\BankCode::getUrl('001');

贡献

有一些辅助脚本能通过composer调用,例如

  • 静态代码分析:php composer.phar run check
  • 代码检查:php composer.phar run lint
  • 测试:php composer.phar run test

注意:要运行代码检查,您必须首先从这里下载规则集。

许可协议

此库采用MIT许可协议