已部署的平面文件解析器
v0.3.0
2017-02-03 03:19 UTC
Requires
- php: >=5.6
Requires (Dev)
- codeclimate/php-test-reporter: ^0.1.2
- phpunit/phpunit: 4.6.*
This package is not auto-updated.
Last update: 2024-09-14 19:47:38 UTC
README
已部署的平面文件解析器
使用说明
配置
$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', ], ], ], ]);
解析
$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" // } // }
组合
$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"