axelero/fixed-width

一个用于解析和构建固定宽度字符串的库

1.01 2017-01-09 09:48 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:38:38 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads Coverage Status

这是一个简单的库,用于读取和写入固定宽度文件。它是从这个Python库部分迁移过来的,目标是简单、稳定和一致。

安装

通过Composer

$ composer require axelero/fixed-width

用法

您可以读取任意字符串中的值

$config = [
'a' => [
    'type' => 'string',
    'start' => 3,
    'length' => 10
],
// numeric indexes of the config array will be ignored
[
    'type' => 'string',
    'start' => 13,
    'end' => 13
],
'b' => [
    'type' => 'string',
    'start' => 14,
    'end' => 17
]
];

$obj = new FixedWidth($config);

$line = '12345678901234567890';
$record = $obj->readLine($line); // ['a' => '3456789012','b' => '4567',]

您可以将数组写入固定宽度的字符串

$config = [
    'a' => [
        'type' => 'string',
        'start' => 1,
        'end' => 5
    ],
    'b' => [
        'type' => 'integer',
        'start' => 6,
        'end' => 10
    ]
];

$obj = new FixedWidth($config);

$data = ['a' => 'xxx', 'b' => 42];
$string = $obj->writeLine($data); // 'xxx  00042'

每个字段的可能配置值有

  • type: 字符串|整数
  • alignment: 左|右 (默认为字符串左对齐,整数右对齐)
  • padding: 用于填充缺失空间的字符 (默认为字符串'',整数'0')
  • default: 当数组字段缺失时写入的内容 (默认为字符串'',整数'0')

测试

$ phpunit

贡献

欢迎您发送任何PR。请确保测试通过。请尽量保持代码PSR合规(项目的根目录中有一个.php_cs配置文件用于此)。

致谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。