phpcfdi / cfdi-to-json
将 CFDI 转换为 JSON
Requires
- php: >=7.3
- ext-dom: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.5
README
将 CFDI 文件转换为 JSON 工具
关于 phpcfdi/cfdi-to-json
这是一个工具,它遵循自己的约定,将 CFDI(SAT XML)文件转换为 JSON 格式。
遵循的一些约定包括:
- 元素是包含其值、属性及其子元素的对象。
- 可能多次出现的元素被处理为数组。
- 库内部记录了可能多次出现的元素。
安装
使用 composer
composer require phpcfdi/cfdi-to-json
基本用法
从 CFDI(字符串)转换为 JSON(字符串)
<?php declare(strict_types=1); use PhpCfdi\CfdiToJson\JsonConverter; $contents = file_get_contents('mi-archivo-xfdi.xml'); $json = JsonConverter::convertToJson($contents); echo $json;
从 DOMDocument
转换为 array
<?php declare(strict_types=1); use PhpCfdi\CfdiToJson\Factory; /** @var DOMDocument $document */ $factory = new Factory(); $dataConverter = $factory->createConverter(); $rootNode = $dataConverter->convertXmlDocument($document); $array = $rootNode->toArray(); var_export($array);
输出示例
注意:
Emisor
看起来是主对象的属性,但实际上是一个对象,而不是字符串。Concepto
包含一个对象数组,每个对象都是节点概念的表示。Traslado
尽管只包含一个对象,但作为一个数组,因为它已知是多个。Complemento
是一个数组,尽管在附件 20 中有定义,因为 XSD 指出它可以有多个出现。
{ "Certificado": "MIIGH...imAyX", "CondicionesDePago": "CONTADO", "Fecha": "2018-01-12T08:15:01", "Folio": "11541", "FormaPago": "04", "LugarExpedicion": "76802", "MetodoPago": "PUE", "Moneda": "MXN", "NoCertificado": "00001000000401220451", "Sello": "Xt7tK...gdg==", "Serie": "H", "SubTotal": "1709.12", "TipoDeComprobante": "I", "Total": "2010.01", "Version": "3.3", "xsi:schemaLocation": "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd http://www.sat.gob.mx/implocal http://www.sat.gob.mx/sitio_internet/cfd/implocal/implocal.xsd", "Emisor": { "Nombre": "PROMOTORA OTIR SA DE CV", "RegimenFiscal": "601", "Rfc": "POT9207213D6" }, "Receptor": { "Nombre": "DAY INTERNATIONAL DE MEXICO SA DE CV", "Rfc": "DIM8701081LA", "UsoCFDI": "G03" }, "Conceptos": { "Concepto": [ { "Cantidad": "2.00", "ClaveProdServ": "90111501", "ClaveUnidad": "E48", "Descripcion": "Paquete", "Importe": "1355.67", "Unidad": "UNIDAD DE SERVICIO", "ValorUnitario": "677.83", "Impuestos": { "Traslados": { "Traslado": [ { "Base": "1355.67", "Importe": "216.91", "Impuesto": "002", "TasaOCuota": "0.160000", "TipoFactor": "Tasa" } ] } } }, { "Cantidad": "1.00", "ClaveProdServ": "90101501", "ClaveUnidad": "E48", "Descripcion": "Restaurante", "Importe": "353.45", "Unidad": "UNIDAD DE SERVICIO", "ValorUnitario": "353.45", "Impuestos": { "Traslados": { "Traslado": [ { "Base": "353.45", "Importe": "56.55", "Impuesto": "002", "TasaOCuota": "0.160000", "TipoFactor": "Tasa" } ] } } } ] }, "Impuestos": { "TotalImpuestosTrasladados": "273.46", "Traslados": { "Traslado": [ { "Importe": "273.46", "Impuesto": "002", "TasaOCuota": "0.160000", "TipoFactor": "Tasa" } ] } }, "Complemento": [ { "ImpuestosLocales": { "TotaldeRetenciones": "0.00", "TotaldeTraslados": "27.43", "version": "1.0", "TrasladosLocales": [ { "ImpLocTrasladado": "IH", "Importe": "27.43", "TasadeTraslado": "2.50" } ] }, "TimbreFiscalDigital": { "FechaTimbrado": "2018-01-12T08:17:54", "NoCertificadoSAT": "00001000000406258094", "RfcProvCertif": "DCD090706E42", "SelloCFD": "Xt7tK...gdg==", "SelloSAT": "IRy7w...6Zg==", "UUID": "CEE4BE01-ADFA-4DEB-8421-ADD60F0BEDAC", "Version": "1.1", "xsi:schemaLocation": "http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigitalv11.xsd" } } ] }
内部功能
转换从 DOMDocument
对象开始,逐个节点遍历,并在每个转换中生成一个包含其基本属性(名称、路径、文本值、属性和子代)的 Nodes\Node
类型对象。子代(Nodes\Children
)是一组 Nodes\Node
节点。
在导出到数组时,是 Nodes\Node::toArray()
决定节点是直接添加到对象中,还是作为对象数组。
多次出现的元素
为了检测多次出现的元素,这个库包含一个名为 src/UnboundedOccursPaths.json
的文件,其中列出了可能多次出现的元素路径。
此列表可以通过使用文件 bin/max-occurs-paths.php
生成,该文件将下载 PhpCfdi phpcfdi/sat-ns-registry
的命名空间注册记录以及所有包含 maxOccurs="unbounded"
的 XSD 文件,以解释包含这些路径的路径。
自 2021-03-22 以来,已从 phpcfdi/sat-ns-registry
添加一个事件,以通知此存储库命名空间注册记录已更改。
包含文本的节点
XML 节点中包含的文本或值导出到 JSON 结果中的空字符串键。例如,以下 XML:
<detallista:orderIdentification> <detallista:referenceIdentification type="ON">3</detallista:referenceIdentification> </detallista:orderIdentification>
生成以下 JSON:
{ "orderIdentification": { "referenceIdentification": { "": "3", "type": "ON" } } }
支持
您可以在 Github 上创建一个工单来获取支持。
此外,这个库属于 PhpCfdi 社区,因此您可以使用相同的通信渠道从社区成员那里获得帮助。
兼容性
这个库将保持与至少拥有最新活跃支持的PHP版本的兼容性。
我们还使用了语义化版本2.0.0,因此你可以放心使用这个库,而不用担心破坏你的应用程序。
贡献
欢迎贡献。请查阅CONTRIBUTING获取更多详细信息,并请记得检查待办事项文件TODO和变更日志文件CHANGELOG。
版权和许可证
phpcfdi/cfdi-to-json
库版权所有© PhpCfdi,并使用MIT许可证(MIT)许可。请参阅LICENSE获取更多信息。