friendsofcxml/cxml-php
Composer 安装命令:
composer require friendsofcxml/cxml-php
包简介
PHP Implementation of cXML Standard
README 文档
README
cXML is a streamlined protocol intended for consistent communication of business documents between procurement applications, e-commerce hubs and suppliers. http://cxml.org/
cXML Reference Guide (PDF): http://xml.cxml.org/current/cXMLReferenceGuide.pdf
Getting Started
Installation
$ composer require friendsofcxml/cxml-php
Then include Composer’s autoloader:
require_once 'vendor/autoload.php';
Get current dtd definition files
- Download get current Specification from http://cxml.org/downloads.html
- Extract files
- Use cXML.dtd for validation (see below)
Quickstart
Identity and credentials
//we use a basic registry here. You could use your own (db-based?) repository that implements CredentialRepositoryInterface $credentialRegistry = new \CXml\Credential\Registry(); $someSupplier = new \CXml\Model\Credential('DUNS', 12345); $credentialRegistry->registerCredential($someSupplier); $someBuyer = new \CXml\Model\Credential('my-id-type', "buyer@domain.com"); $credentialRegistry->registerCredential($someBuyer); $someHub = new \CXml\Model\Credential('my-id-type', "hub@domain.com", "abracadabra"); $credentialRegistry->registerCredential($someHub);
Register Handler
$handlerRegistry = new \CXml\Handler\HandlerRegistry(); $handlerRegistry->register(new CXml\Handler\Request\SelfAwareProfileRequestHandler(...)); $handlerRegistry->register(new CXml\Handler\Request\StaticStartPagePunchOutSetupRequestHandler(...)); $handlerRegistry->register(new MyOrderRequestHandler()); $handlerRegistry->register(new MyStatusUpdateRequestHandler()); ...
Build cXML
//$payload = new \CXml\Model\Message\...Message(...); //or... //$payload = new \CXml\Model\Request\...Request(...); //or... $payload = new \CXml\Model\Response\...Response(...); $cXml = \CXml\Builder::create() ->payload($payload) ->build(); $payload = new \CXml\Model\Request\...Request(...); $cXml = \CXml\Builder::create() ->payload($payload) ->from(...) ->to(...) ->sender(...) ->build();
Register outgoing cXML documents
You may want to register sent-out documents so they can be referenced by subsequent request-documents via payloadId.
$documentRegistory = new MyDocumentRegistry(); //implements CXml\Document\DocumentRegistryInterface $documentRegistory->register($cXml);
Process incoming cXML documents
$headerProcessor = new \CXml\Processor\HeaderProcessor($credentialRegistry, $credentialRegistry); $cXmlProcessor = new \CXml\Processor\Processor( $headerProcessor, $handlerRegistry, $builder ); $cXmlProcessor->process($cXml);
Putting it all together
$credentialRegistry = new \CXml\Credential\Registry(); //TODO register credentials... $handlerRegistry = new \CXml\Handler\HandlerRegistry(); //TODO register handler... $builder = \CXml\Builder::create(); $headerProcessor = new \CXml\Processor\HeaderProcessor($credentialRegistry, $credentialRegistry); $cXmlProcessor = new \CXml\Processor\Processor( $headerProcessor, $handlerRegistry, $builder ); $pathToDtd = '.'; //point the directory with extracted contents of zip-file with the DTDs, downloaded from cxml.org $dtdValidator = \CXml\Validation\DtdValidator::fromDtdDirectory($pathToDtd); $serializer = \CXml\Serializer::create(); $endpoint = new \CXml\Endpoint( $serializer, $dtdValidator, $cXmlProcessor ); //$xmlString could be the body of an incoming http request $xmlString = '<cXML>...</cXML>'; $result = $endpoint->parseAndProcessStringAsCXml($xmlString); //$result could be null (i.e. for a Response or Message) or another CXml object which would be the Response to a Request //you would have to handle the transport yourself
Handling Date vs DateTime
The cXML specification is not perfectly clear about the format of dates and times. The specification says that dates should be formatted "in the restricted subset of ISO 8601". That means that the format could either be a full ISO 8601 format with time and timezone information (i.e. 2015-04-14T13:36:00-08:00) or a format without time and timezone (2015-04-14).
With some fields the actual time of day is not relevant and could lead to confusion. For example, the
requestedDeliveryDate field in ItemOut. Real-world experience shows that here it is common to only specify the date.
Althout one could argue that the time of day is still relevant here for real tight on-point deliveries.
To solve this problem we introduced a determined CXml\Model\Date class in case of using an explicit
date (without time). This class extends DateTime and is therefore compatible with the rest of the model. The class
enforces a date-only representation (Y-m-d).
Serialization
You should use the CXml\Model\Date class when generating your object-graph in cases you want to output a date-only
value.
Deserialization
When parsing a date-property from a cXML document, the CXml\Model\Date will be instantiated if a date-only
value was discovered (Y-m-d).
Extending cXML
Add custom elements
The definition of cXML is open for extension. There are ways to extend the DTD with overriding existing variables and therefore adding custom elements. With version 2.1.0 we introduced a way to add custom elements to the cXML model.
To make this happen, we have to build our own DTD file and import the original DTD file in it. We can then add our own elements and attributes in the variables that are defined in the original DTD file.
TODO this is only really implemented for the Payment node at the moment.
Example
An example of a custom DTD file that adds a custom element to the PaymentReference element:
<!ENTITY % cxml.payment "( PCard | PaymentToken | PaymentReference* )">
<!ENTITY % elements SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.063/cXML.dtd">
%elements;
<!ELEMENT PaymentReference (Money, IdReference*, Extrinsic*)>
<!ATTLIST PaymentReference
method CDATA #REQUIRED
provider CDATA #IMPLIED
>
To use this DTD file for validation as well as for serialization and deserialization, you could save the file next to the
other DTD files from cXML and use DtdValidator::fromDtdDirectory just as you would with the original DTD files. Or you
could explicitly load only the new DTD file with new DtdValidator($arrayOfDtdFilepaths).
Also you would probably want newly generated cXML files to point to your DTD file. You can do this by telling the
serializer to use your DTD file: Serializer::create('http://...publicUrlToYourDtd').
Now the new element also has to be known by the serializer. Usually the model classes can be found in CXml\Model.
friendsofcxml/cxml-php 适用场景与选型建议
friendsofcxml/cxml-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 144.76k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2022 年 05 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「data」 「e-commerce」 「commerce」 「edi」 「cxml」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 friendsofcxml/cxml-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 friendsofcxml/cxml-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 friendsofcxml/cxml-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Implementation of the Omnibus Directive for Sylius application.
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
Livewire starter kit for Lunar e-commerce.
Load DOM document safety
统计信息
- 总下载量: 144.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 32
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-19