A fielded flat file parser

v0.3.0 2017-02-03 03:19 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:35:42 UTC


README

A fielded flat file parser

Build Status Code Climate Test Coverage

Usage

Configuration

$formats = (new Tummy\Config\Factory())->create([
    'format1' => [
        'ident' => new Tummy\Record\Ident\Match('NEW'), // used for supporting multiple record formats in a single file
        'elements' => [
            [
                'length' => 3,
                'reference' => 'type',
                'required' => true,
            ],
            [
                'length' => 1, // elements w/o reference will be ignored
            ],
            [
                'length' => 16,
                'reference' => 'username',
                'paddingChar' => '-',
                'paddingDirection' => \STR_PAD_LEFT,
            ],
            [
                'length' => 8,
                'reference' => 'birthday',
                'converter' => new Tummy\Record\Element\Converter\DateTime('dmY'),
            ],
        ],
    ],
    'format2' => [
        'ident' => new Tummy\Record\Ident\Match('PWD'),
        'recordClass' => PwdRecord::class, // will use \stdClass if not specified
        'elements' => [
            [
                'length' => 3,
                'reference' => 'type',
            ],
            [
                'length' => 1,
            ],
            [
                'length' => 16,
                'reference' => 'username',
            ],
            [
                'length' => 16,
                'reference' => 'password',
            ],
        ],
    ],
]);

Parse

$parser = new Tummy\Parser($formats);

$records = $parser->parse([
    'NEW -----marcojetson31121985',
    'PWD marcojetson     peterete        ',
]);

var_dump($records);

// array(2) {
//   [0]=>
//   object(stdClass)#18 (3) {
//     ["type"]=>
//     string(3) "NEW"
//     ["username"]=>
//     string(11) "marcojetson"
//     ["birthday"]=>
//     object(DateTime)#19 (3) {
//       ["date"]=>
//       string(26) "1985-12-31 23:20:54.000000"
//       ["timezone_type"]=>
//       int(3)
//       ["timezone"]=>
//       string(13) "Europe/Berlin"
//     }
//   }
//   [1]=>
//   object(PwdRecord)#20 (3) {
//     ["type"]=>
//     string(3) "PWD"
//     ["username"]=>
//     string(11) "marcojetson"
//     ["password"]=>
//     string(8) "peterete"
//   }
// }

Compose

$record = new \stdClass();
$record->type = 'NEW';
$record->username = 'marcojetson';
$record->birthday = new \DateTime('1985-12-31');

$composer = new Tummy\Composer($formats['format1']);
echo $composer->compose([$record])[0];

// "NEW -----marcojetson31121985"