sabas/edifact 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sabas/edifact

Composer 安装命令:

composer require sabas/edifact

包简介

Tools in PHP for UN/EDIFACT

README 文档

README

Tools to process EDI messages in UN/EDIFACT format

Supported syntax is version 3.

It's provided in a Composer package:

composer require sabas/edifact

The mapping XML files are provided in a separate package:

composer require php-edifact/edifact-mapping

EDI\Parser

Given an EDI message checks the syntax, outputs errors and returns the message as a multidimensional array.

INPUT

$p = new EDI\Parser();
$p->load($location);     // a local path to a file or a URL (if file_get_contents() allows for remote access)
$p->loadString($string); // a full message (single line text)
$p->loadArray($array);   // an array of strings (one segment per entry)

OUTPUT

Errors

$c->errors();

Array

$c->get();

EDI\Encoder

Given a multidimensional array (formatted as the output of the parser), returns an EDI string, optionally one segment per line.

INPUT

$c = new EDI\Encoder($x, $compact = true);

$x is a multidimensional array where first dimension is the EDI segment, second contains elements:

  • single value
  • array (representing composite elements)

$compact is a boolean, if you need a segment per line. Set to false to enable wrapped lines.

OR

$c = new EDI\Encoder();
$c->encode($array, false);

OUTPUT

$c->get(); // returns String

EDI\Analyser

Create human-readable, structured text with comments from segments.xml. Requires the EDI\Mapping package.

        $parser = new EDI\Parser();
        $parser->load($file);
        $segments = $parser->getRawSegments();
        $parsed = $parser->get();
        $analyser = new EDI\Analyser();
        $mapping = new EDI\MappingProvider('D95B');
        $analyser->loadSegmentsXml($mapping->getSegments());
        $analyser->loadSegmentsXml($mapping->getServiceSegments(), false);
        $analyser->loadMessageXml($mapping->getMessage('coparn'));
        $analyser->loadCodesXml($mapping->getCodes());
        $analyser->directory = 'D95B';
        $result = $analyser->process($parsed, $segments);                   
  • $file is the path to orginal EDI message file

Example INPUT

UNA:+,? '
UNB+UNOA:1+MAEU+LVRIXBCT+200813:0816+1412605'
UNH+141260500001+COPARN:D:95B:UN'
BGM+12+20200813081626+9'
RFF+BN:204549739'
NAD+CA+MAE:172:20'
EQD+CN++45G1:102:5+2+2+4'
RFF+BN:204549739'
RFF+SQ:7G3JTL39O0M3B'
TMD+++2'
DTM+201:202008130000:203'
LOC+98+LVRIX:139:6+RIGA TERMINAL:TER:ZZZ'
CNT+16:1'
UNT+12+141260500001'
UNZ+1+1412605'

Example Output


UNA:+,? '
UNB - InterchangeHeader
  (To start, identify and specify an interchange)
  [0] UNOA,1
      unb1 - syntaxIdentifier
      Syntax identifier
    [0] UNOA
        id: unb11 - syntaxIdentifier
        Syntax identifier
        type: a
        required: true
        length: 4
    [1] 1
        id: unb12 - syntaxVersionNumber
        Syntax version number
        type: n
        required: true
        length: 1
  [1] MAEU
      unb2 - interchangeSender
      Interchange sender
  [2] LVRIXBCT
      unb3 - interchangeRecipient
      Interchange recipient
  [3] 200813,0816
      unb4 - dateTimePreparation
      Date Time of preparation
    [0] 200813
        id: unb41 - date
        
        type: n
        required: true
        length: 6
    [1] 0816
        id: unb42 - time
        
        type: n
        required: true
        length: 4
  [4] 1412605
      unb5 - interchangeControlReference
      

UNB+UNOA:1+MAEU+LVRIXBCT+200813:0816+1412605'
UNH - messageHeader http://www.unece.org/trade/untdid/d95b/trsd/trsdunh.htm
  (To head, identify and specify a message.)
  [0] 141260500001
      unh1 - messageReferenceNumber
      
  [1] COPARN,D,95B,UN
      unh2 - messageType
      
    [0] COPARN
        id: unh21 - messageType
        
        type: an
        maxlen: 6
        required: true
    [1] D
        id: unh22 - messageVersion
        
        type: an
        maxlen: 3
        required: true
    [2] 95B
        id: unh23 - messageRelease
        
        type: an
        maxlen: 3
        required: true
    [3] UN
        id: unh24 - controllingAgency
        
        type: an
        maxlen: 3
        required: true

UNH+141260500001+COPARN:D:95B:UN'
BGM - beginningOfMessage http://www.unece.org/trade/untdid/d95b/trsd/trsdbgm.htm
  (To indicate the type and function of a message and to transmit the
  identifying number.)
  [0] 12
      C002 - documentmessageName
      Identification of a type of document/message by code or name. Code
      preferred.
  [1] 20200813081626
      1004 - documentmessageNumber
      Reference number assigned to the document/message by the issuer.
  [2] 9
      1225 - messageFunctionCoded
      Code indicating the function of the message.

BGM+12+20200813081626+9'
RFF - reference http://www.unece.org/trade/untdid/d95b/trsd/trsdrff.htm
  (To specify a reference.)
  [0] BN,204549739
      C506 - reference
      Identification of a reference.
    [0] BN - Booking reference number
        id: 1153 - referenceQualifier
        Code giving specific meaning to a reference segment or a reference
        number.
        type: an
        maxlen: 3
        required: true
    [1] 204549739
        id: 1154 - referenceNumber
        Identification number the nature and function of which can be
        qualified by an entry in data element 1153 Reference qualifier.
        type: an
        maxlen: 35

RFF+BN:204549739'
NAD - nameAndAddress http://www.unece.org/trade/untdid/d95b/trsd/trsdnad.htm
  (To specify the name/address and their related function, either by CO82 only
  and/or unstructured by CO58 or structured by CO80 thru 3207. be permitted
  to use the ADR segment and/or the PNA segment.  After the conclusion of the
  Brazil JRT (scheduled for April 1996), this NAD segment shall NO LONGER BE
  PERMITTED FOR USE IN ANY NEW MESSAGES submitted for Status 1 in the
  UN/EDIFACT Directories. This means that either the ADR and/or the PNA
  segments shall be used in place of this NAD segment by the message
  designers. (See Rapporteurs' decision on the proposed ADR & PNA segments
  and the existing NAD segment - Sydney, April 1995).)
  [0] CA
      3035 - partyQualifier
      Code giving specific meaning to a party.
  [1] MAE,172,20
      C082 - partyIdentificationDetails
      Identification of a transaction party by code.
    [0] MAE
        id: 3039 - partyIdIdentification
        Code identifying a party involved in a transaction.
        type: an
        maxlen: 35
        required: true
    [1] 172 - Carrier code
        id: 1131 - codeListQualifier
        Identification of a code list.
        type: an
        maxlen: 3
    [2] 20 - BIC (Bureau International des Containeurs)
        id: 3055 - codeListResponsibleAgencyCoded
        Code identifying the agency responsible for a code list.
        type: an
        maxlen: 3

NAD+CA+MAE:172:20'
EQD - equipmentDetails http://www.unece.org/trade/untdid/d95b/trsd/trsdeqd.htm
  (To identify a unit of equipment.)
  [0] CN
      8053 - equipmentQualifier
      Code identifying type of equipment.
  [1] 
      C237 - equipmentIdentification
      Marks (letters and/or numbers) identifying equipment used for transport
      such as a container.
  [2] 45G1,102,5
      C224 - equipmentSizeAndType
      Code and/or name identifying size and type of equipment used in
      transport. Code preferred.
    [0] 45G1
        id: 8155 - equipmentSizeAndTypeIdentification
        Coded description of the size and type of equipment e.g. unit load
        device.
        type: an
        maxlen: 10
    [1] 102 - Size and type
        id: 1131 - codeListQualifier
        Identification of a code list.
        type: an
        maxlen: 3
    [2] 5 - ISO (International Organization for Standardization)
        id: 3055 - codeListResponsibleAgencyCoded
        Code identifying the agency responsible for a code list.
        type: an
        maxlen: 3
  [3] 2
      8077 - equipmentSupplierCoded
      To indicate the party that is the supplier of the equipment.
  [4] 2
      8249 - equipmentStatusCoded
      Indication of the action related to the equipment.
  [5] 4
      8169 - fullemptyIndicatorCoded
      To indicate the extent to which the equipment is full or empty.

EQD+CN++45G1:102:5+2+2+4'
RFF - reference http://www.unece.org/trade/untdid/d95b/trsd/trsdrff.htm
  (To specify a reference.)
  [0] BN,204549739
      C506 - reference
      Identification of a reference.
    [0] BN - Booking reference number
        id: 1153 - referenceQualifier
        Code giving specific meaning to a reference segment or a reference
        number.
        type: an
        maxlen: 3
        required: true
    [1] 204549739
        id: 1154 - referenceNumber
        Identification number the nature and function of which can be
        qualified by an entry in data element 1153 Reference qualifier.
        type: an
        maxlen: 35

RFF+BN:204549739'
RFF - reference http://www.unece.org/trade/untdid/d95b/trsd/trsdrff.htm
  (To specify a reference.)
  [0] SQ,7G3JTL39O0M3B
      C506 - reference
      Identification of a reference.
    [0] SQ - Container sequence number
        id: 1153 - referenceQualifier
        Code giving specific meaning to a reference segment or a reference
        number.
        type: an
        maxlen: 3
        required: true
    [1] 7G3JTL39O0M3B
        id: 1154 - referenceNumber
        Identification number the nature and function of which can be
        qualified by an entry in data element 1153 Reference qualifier.
        type: an
        maxlen: 35

RFF+SQ:7G3JTL39O0M3B'
TMD - transportMovementDetails http://www.unece.org/trade/untdid/d95b/trsd/trsdtmd.htm
  (To specify transport movement details for a goods item or equipment.)
  [0] 
      C219 - movementType
      Description of type of service for movement of cargo.
  [1] 
      8332 - equipmentPlan
      Description indicating equipment plan, e.g. FCL or LCL.
  [2] 2
      8341 - haulageArrangementsCoded
      Specification of the type of equipment haulage arrangements.

TMD+++2'
DTM - datetimeperiod http://www.unece.org/trade/untdid/d95b/trsd/trsddtm.htm
  (To specify date, and/or time, or period.)
  [0] 201,202008130000,203
      C507 - datetimeperiod
      Date and/or time, or period relevant to the specified date/time/period
      type.
    [0] 201 - Pick-up date/time of equipment
        id: 2005 - datetimeperiodQualifier
        Code giving specific meaning to a date, time or period.
        type: an
        maxlen: 3
        required: true
    [1] 202008130000
        id: 2380 - datetimeperiod
        The value of a date, a date and time, a time or of a period in a
        specified representation.
        type: an
        maxlen: 35
    [2] 203 - CCYYMMDDHHMM
        id: 2379 - datetimeperiodFormatQualifier
        Specification of the representation of a date, a date and time or of
        a period.
        type: an
        maxlen: 3

DTM+201:202008130000:203'
LOC - placelocationIdentification http://www.unece.org/trade/untdid/d95b/trsd/trsdloc.htm
  (To identify a country/place/location/related location one/related location
  two.)
  [0] 98
      3227 - placelocationQualifier
      Code identifying the function of a location.
  [1] LVRIX,139,6
      C517 - locationIdentification
      Identification of a location by code or name.
    [0] LVRIX
        id: 3225 - placelocationIdentification
        Identification of the name of place/location, other than 3164 City
        name.
        type: an
        maxlen: 25
    [1] 139 - Port
        id: 1131 - codeListQualifier
        Identification of a code list.
        type: an
        maxlen: 3
    [2] 6 - UN/ECE (United Nations - Economic Commission for Europe)
        id: 3055 - codeListResponsibleAgencyCoded
        Code identifying the agency responsible for a code list.
        type: an
        maxlen: 3
  [2] RIGA TERMINAL,TER,ZZZ
      C519 - relatedLocationOneIdentification
      Identification the first related location by code or name.
    [0] RIGA TERMINAL
        id: 3223 - relatedPlacelocationOneIdentification
        Specification of the first related place/location by code.
        type: an
        maxlen: 25
    [1] TER
        id: 1131 - codeListQualifier
        Identification of a code list.
        type: an
        maxlen: 3
    [2] ZZZ - Mutually defined
        id: 3055 - codeListResponsibleAgencyCoded
        Code identifying the agency responsible for a code list.
        type: an
        maxlen: 3

LOC+98+LVRIX:139:6+RIGA TERMINAL:TER:ZZZ'
CNT - controlTotal http://www.unece.org/trade/untdid/d95b/trsd/trsdcnt.htm
  (To provide control total.)
  [0] 16,1
      C270 - control
      Control total for checking integrity of a message or part of a message.
    [0] 16 - Total number of equipment
        id: 6069 - controlQualifier
        Determines the source data elements in the message which forms the
        basis for 6066 Control value.
        type: an
        maxlen: 3
        required: true
    [1] 1
        id: 6066 - controlValue
        Value obtained from summing the values specified by the Control
        Qualifier throughout the message (Hash total).
        type: n
        maxlen: 18
        required: true

CNT+16:1'
UNT - MessageTrailer http://www.unece.org/trade/untdid/d95b/trsd/trsdunt.htm
  (MessageTrailer)
  [0] 12
      9900 - segmentsNumber
      segmentsNumber
  [1] 141260500001
      9901 - msgRefNumber
      msgRefNumber

UNT+12+141260500001'
UNZ - InterchangeTrailer http://www.unece.org/trade/untdid/d95b/trsd/trsdunz.htm
  (InterchangeTrailer)
  [0] 1
      9902 - interchangeControlCount
      interchangeControlCount
  [1] 1412605
      9903 - interchangeControlRef
      interchangeControlRef

EDI\Reader

Read specific segment element values from parsed EDI file. Pass the reader a parser with a message already loaded.

INPUT

$p = new EDI\Parser();
$p->load($x);
$r = new EDI\Reader($p);
$sender = $r->readEdiDataValue('UNB', 2);
$Dt = $r->readUNBDateTimeOfPreparation();

See section about EDI\Parser above on how to load a file into a parser.

OUTPUT

Errors

$r->errors();

Array

$r->getParsedFile();

EDI\Interpreter

Organizes the data parsed by EDI/Parser using the XML description of the message and the XML segments.

INPUT

$p = new EDI\Parser();
$p->load($edifile);
$edi = $p->get();

$mapping = new EDI\Mapping\MappingProvider('D95B');

$analyser = new EDI\Analyser();
$segs = $analyser->loadSegmentsXml($mapping->getSegments());
$svc = $analyser->loadSegmentsXml($mapping->getServiceSegments(3));

$interpreter = new EDI\Interpreter($mapping->getMessage('CODECO'), $segs, $svc);
$prep = $interpreter->prepare($edi);

OUTPUT

JSON

$interpreter->getJson()

JSON for interchange service segments (UNB / UNZ)

$interpreter->getJsonServiceSegments()

Errors (per message)

$interpreter->getErrors()

Example

Edifact

DTM+7:201309200717:203'

Array

['DTM',['7','201309200717','203']]

Testing

The package should be required with composer, alongside edifact-mapping. The tests then can be run simply with phpunit in the root of the package.

Notes

Valid characters are: A-Za-z0-9.,-()/'+:=?!"%&*;<> UNECE

sabas/edifact 适用场景与选型建议

sabas/edifact 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.72M 次下载、GitHub Stars 达 284, 最近一次更新时间为 2014 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「container」 「message」 「edi」 「edifact」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sabas/edifact 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sabas/edifact 我们能提供哪些服务?
定制开发 / 二次开发

基于 sabas/edifact 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.72M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 285
  • 点击次数: 27
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 284
  • Watchers: 27
  • Forks: 90
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2014-06-20