uhin/x12-parser

用于解析X12 EDI的库

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