A simple library to read and write fixed width file. It's a partial port from this python library, with the aim to be simple, stable and consistent.
Via Composer
$ composer require axelero/fixed-width
You can read values from arbitrary strings:
$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',]
You can write arrays into fixed-width strings:
$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'
The possible configuration values for each field are:
- type: string|integer
- alignment: left|right (defaults to left for string, right for integers)
- padding: charachter to use to fill the missing space (defaults to '' for string, '0' for integers)
- default: what to write when an array field is missing (defaults to '' for string, '0' for integers)
$ phpunit
You are welcome to send any PR. Please make sure the tests pass. Please try to keep the code PSR compliant (in the root of the project lies a .php_cs config file for that).
The MIT License (MIT). Please see License File for more information.