axelero / fixed-width
A library to parse and build fixed-width strings
Installs: 28 353
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 6
Forks: 2
Open Issues: 0
Requires
- php: >=5.5.9
Requires (Dev)
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2024-12-16 15:39:10 UTC
README
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.
Install
Via Composer
$ composer require axelero/fixed-width
Usage
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)
Testing
$ phpunit
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).
Credits
License
The MIT License (MIT). Please see License File for more information.