FixedWidth
![Software License][ico-license]
![Total Downloads][ico-downloads]
, (*1)
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., (*2)
Install
Via Composer, (*3)
``` bash
$ composer require axelero/fixed-width, (*4)
## Usage
You can read values from arbitrary strings:
``` php
$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:, (*5)
$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:, (*6)
-
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)
Testing
bash
$ phpunit
, (*7)
Contributing
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)., (*8)
Credits
License
The MIT License (MIT). Please see License File for more information., (*9)