bennett-treptow/x12-parser

一个用于解析X12 EDI的库。由uhin/x12-parser分支而来

1.0.10 2020-07-23 18:20 UTC

README

版本

1.0.0
  • 首次发布
  • 基本解析和序列化X12
  • EDI存储在分层数据结构中

包括此库

为了使用此库,您需要在项目的composer.json文件中添加以下内容到repositories部分

{
  ...
  "require": {
    ...
    "uhin/x12-parser": "1.0.0"
    ...
  }
}

现在,您可以通过运行以下命令将库包含到项目中

composer require uhin/x12-parser:1.0.0

将库包含到项目中后,您现在可以开始编写代码以解析和序列化X12 EDI。

示例

解析文件
use Uhin\X12Parser\Parser\X12Parser;

// Load the file into memory
$rawX12 = file_get_contents('path-to-your-file');

// Create a parser object
$parser = new X12Parser($rawX12);

// Parse the file into an object data structure
$x12 = $parser->parse();
// $x12 is now an X12 object containing all of the information from the X12 file
访问/修改X12中的数据

注意:如果任何段/属性可以有多个,则无论是否有多个,都会解析为一个数组。这是为了避免在处理数组或对象时产生混淆。

// Using the $x12 object from the examlpe above...

// Retrieving the GS06
$gs06 = $x12->ISA[0]->GS[0]->GS06;

// Setting the GS06 value
$x12->ISA[0]->GS[0]->GS06 = 'Changed Value';
将解析的文件转换回X12
use Uhin\X12Parser\Serializer\X12Serializer;

// Using the $x12 object from the examlpe above...
$serializer = new X12Serializer($x12);

// Options for serializing:
$serializer->addNewLineAfterSegment(true);
$serializer->addNewLineAfterIEA(true);

// Generate the raw X12 string
$rawX12 = $serializer->serialize();

单元测试

首先,请确保您的测试文件已放置在tests/test-files目录中。

注意: 如果这些文件包含任何HIPAA数据,请不要将它们提交到git中。

当前测试将在tests/test-files目录中查找以下文件

  • 277.txt
  • 835.txt
  • 837.txt
  • 999.txt
  • TA1.txt

一旦您的文件就绪,您可以从库的根目录运行以下命令

./vendor/bin/phpunit --bootstrap ./vendor/autoload.php --testdox tests